Hallo,
folgendes möchte ich mit meinem Skript erreichen:
Das Skript soll auf einem Terminalserver ausgeführt werden. Dabei soll es verschiedene commands aus der Funktion remote auf 20+ anderen Servern ausführen und den Output dann in eine file schreiben, sodass ich am Ende eine Datei habe mit dem Output von allen Servern.
Ich habe das hier mal wie folgt probiert, jedoch wird mir hier eine Fehlermeldung geworfen, da es anscheinend ein Problem mit meinem Invoke-command gibt. Nach Recherche kann ich meinen Fehler allerdings nicht erkennen.
Ich hatte auch bereits einen anderen Ansatz, bei dem ich das was in der Funktion steht in ein anderes Skript auslagere und dieses über den Invoke Command ausführen. Das hatte eigentlich besser funktioniert aber es gab Probleme den Output in eine Datei zu schreiben, da das Skript nicht als Administrator ausgeführt wurde. Dann bekam ich einen Fehler da ich nicht auf den file share zugreifen durfte wo der Output hingeschrieben werden sollte (das ging bei manchen servern aber nicht bei allen). Gibt es hier eine Möglichkeit den INvoke-command als Admin auf dem Server laufen zu lassen?
Oder was ist mein Fehler in dem folgenden Skript?
function get-infofromremoteserver { $systeminfo = Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime $gpresult = gpresult /scope computer /r | findstr "SG_Test" #$systeminfo, $gpresult | out-file -filepath Y:\TKAADMIN\Tetes\dump.txt -append -width 200 Write-Output "GGGGG" return $systeminfo, $gpresult } $c = Get-Credential -Message "Please enter your username and password to access the server" $User = "Admin" #$PWord = ConvertTo-SecureString -String -AsPlainText -Force #$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord $resultsFolderPath = "Y:\TKAADMIN\Tetes\dump.txt" $Path = "Y:\TKAADMIN\Tetes\serverlist.csv" $servers = Import-Csv -Path Y:\TKAADMIN\Tetes\serverlist.csv -Delimiter ";" -Encoding UTF8 Write-Output $servers.server foreach ($server in $servers) { Write-Output "Hafff" $session = New-PSSession -ComputerName $server.server -Credential $c Invoke-Command -Computername $server -ScriptBlock ${function:get-infofromremoteserver} -Session $session | Select-Object systeminfo, gpresult #Invoke-Command -FilePath Y:\TKAADMIN\Justatest.ps1 -Session $session #Write-Output $systeminfo, $gpresult "################ " + $systeminfo.csname + " ################" | Out-file -FilePath $resultsFolderPath -append $systeminfo | Out-file -FilePath $resultsFolderPath -append"GPRESULT: " | Out-file -FilePath $resultsFolderPath -append $gpresult | Out-file -FilePath $resultsFolderPath -append" " | Out-file -FilePath $resultsFolderPath -append"###########################################################" | Out-file -FilePath $resultsFolderPath -append" " | Out-file -FilePath $resultsFolderPath -append }
Fehlermeldung
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings. At Y:\TKAADMIN\ReadOutLastRebootandGPOs.ps1:33 char:5+ Invoke-Command -Credential $c -Computername $server -scriptblock ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand