0
votes

I'm working on a script to create scan folder shortcut on PC (target folder on server) , but PC more than 1000+. The following script works fine as one by one, I would like to increase efficiency and shorten the time. Is there any way I could Invoke-Command them in the same time in a parallel? An example would be appreciated. Thanks

Invoke-Command -ComputerName 'PC001','PC002' -ScriptBlock { Register-PSSessionConfiguration -RunAsCredential 'domain\user' -Name test -Force }

Invoke-Command -ComputerName 'PC001','PC002' -FilePath 'C:\temp\create scan shortcut.ps1' -ConfigurationName test

#get server name and pc name
    $PCname = $env:computername
    $Servername = $PCname.Substring(0,9)+"P001"

#create shortcut on C:\
    $SourceFileLocation = "\\$Servername\Scan"
    $ShortcutLocation = "C:\Scan.lnk"
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation)
    $Shortcut.TargetPath = $SourceFileLocation
    $Shortcut.Save()

#test path
    $path = Test-Path -path "C:\Scan.Lnk"
        if ($path -eq 'True'){ 
            Write-Host "$PCname Scan folder already existed" -ForegroundColor Green 
            }

        else{
            Write-Host "$PCname Scan folder does not exist" -ForegroundColor Red
            } 
1

1 Answers

1
votes

This does run in parallel. It will wait until they all finish, but they run at the same time.

invoke-command localhost,localhost,localhost { sleep 10 }
get-history | select -last 1 | fl


Id                 : 9
CommandLine        : invoke-command localhost,localhost,localhost { sleep 10 }
ExecutionStatus    : Completed
StartExecutionTime : 6/10/2020 12:53:37 PM
EndExecutionTime   : 6/10/2020 12:53:50 PM