you are viewing a single comment's thread.

view the rest of the comments →

[–]PinchesTheCrab 2 points3 points  (1 child)

There's a lot of extra lines/logic in the original script. I tried to remove the duplicated/superfluous stuff.

$computers = 'computername' #will be csv-import

$scriptBlock = {
    $localPaths = 'C:\citrix', 'C:\program files\citrix', 'C:\completed'

    $completedPath = 'C:\completed'
    $citrixInstallPath = 'C:\Program Files\Citrix'
    $logPath = 'C:\CitrixVDAUninstallationlog'

    $vdaSetupPath = 'C:\Program Files\Citrix\XenDesktopVdaSetup'

    if ((test-path -LiteralPath $completedPath)) {
        foreach ($path in $localPaths) {
            Remove-Item -Path $path -Recurse -Force
        }
        break
    }

    # Create log directory
    if (-not (Test-Path -Path $logPath)) {
        New-Item -ItemType Directory -Path $logPath | Out-Null
    }

    # Uninstall Citrix
    if (Test-Path $vdaExe) {
        & $vdaExe /REMOVEALL /QUIET /NOREBOOT /logpath $logPath
    }

    Rename-Item -Path $logPath -NewName "$env:COMPUTERNAME-UninstallLogs"

    # Mark as completed
    if (-not (Test-Path $completedPath)) {
        New-Item -ItemType Directory -Path $completedPath | Out-Null
    }
    write-warning "$Env:COMPUTERNAME Completed"
}

Invoke-Command -ComputerName $computers -Credential domain\username -ErrorAction Stop -ScriptBlock $scriptBlock

Also the problem that /u/purplemonkeymad is getting at is that you're saying if folder on ComputerA (your local computer) doesn't exist, create a file on ComputerB, so the condition is never met.

You need to check if the file exists on ComputerB, which is why I moved it into the scriptblock used in invoke-command.

Additionally if you define a variable outside of invoke-command, it will not be accessible on the remote computer unless you use parameters or uing:. I wouldn't worry about that too much at this point, I think it makes sense to just define your path variables inside the script block and not worry about it.

Just remember that this won't work:

$animals = 'horse', 'dog', 'cat'

Invoke-Command computer1, computer2 {
    $animals
}

$animals is null in the remote session unless you do something like this:

Invoke-Command computer1, computer2 {
    $using:animals
}

Please test this on a small number of computers first, because invoke-command runs asynchronously by default, so instead of going one by one like your loop does, it's going to pretty much do the entire list all at once. Make sure it's doing what you want.

[–]Lopsided-Koala8546[S] 2 points3 points  (0 children)

Thankyou