question

Sara925 avatar image
0 Votes"
Sara925 asked IanXue-MSFT answered

Powershell script to run on multiple servers

I have the below script which needs to be ran against multiple servers in the servers.txt file , tried few things with Invoke command but not able to get the entire script working.

Any ideas are much apprecaited!


$ServerNames = get-content "c:\Temp\servers.txt"
foreach ($servername in $servernames)
{
$servername
$AAClientVersion = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |Get-ItemProperty | where-Object DisplayName -eq "AA 11.3"
If ($AAClientVersion.DisplayVersion -eq "11.3.4.2") {
write-host $AAClientVersion.DisplayName "Current Version is $($AAClientVersion.DisplayVersion )" -BackgroundColor DarkGreen
}
Else {
write-host $AAClientVersion.DisplayName 'Different version exists,Check Version!' -BackgroundColor Red -ErrorAction stop
}
$process = Get-Process 'aa' -ErrorAction SilentlyContinue
if ($process -eq $true) {
write-warning "Stopping process aa"
Stop-process -processname 'aa'
}
elseif($process -eq $null){
Write-warning "process aa is not running"
}
write-warning "Stopping service AA"
Get-Service -DisplayName 'AA*' -ErrorAction Silentlycontinue
Stop-Service -DisplayName 'AA*'
write-warning "AA service is stopped successfully"

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your code is not running on the remote $ServerName. It is running over and over on the local machine for however many entries are in the servers.txt file. You need to use Invoke-Command with a ScriptBlock.

https://www.optimizationcore.com/scripting/invoke-command-powershell-usage-examples-remote-execution/

Start small. Get the script to return just the $AAClientVersion.DisplayVersion so that you can verify that you can successfully access the remote systems and report on the version installed on each computer.

I'm not sure what you are trying to do with processes and services. Typically, if you stop the service, it's process will gracefully end. If you kill the process you could potentially corrupt a data file that the process had open. Depending on what AA does, obviously. Stop the service first, then kill the process if it won't end.

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

You have to enable the second-hop with CredSSP. Please refer to this link
https://devblogs.microsoft.com/scripting/enable-powershell-second-hop-functionality-with-credssp/

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sara925 avatar image
0 Votes"
Sara925 answered

ok that worked ! But there is another line in the script where I try to copy files from network share to local folder using copy-item , I run into object does not exist "UnauthorizedAccessException" although the file exists on the path.

Any ideas?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.