Itxx
asked on
Powershell script to run commands and scripts on remote server(s)
Hello,
I'm very new to powershell and need some advice for improvement.
I'm writing a script to query hyper-v server(s) and information about disk space.
I'm using New-PSSession and the Credssp for the double hop problem.
If I run every command in Powershell manually everything works, but as soon is I run the entire script it fails with errors like:
* Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
* Access to the path '\\serverl\share$\HDDresul t.txt' is denied.
The script:
And the content of HDDinfo.ps1:
I'm very new to powershell and need some advice for improvement.
I'm writing a script to query hyper-v server(s) and information about disk space.
I'm using New-PSSession and the Credssp for the double hop problem.
If I run every command in Powershell manually everything works, but as soon is I run the entire script it fails with errors like:
* Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
* Access to the path '\\serverl\share$\HDDresul
The script:
Enable-WSManCredSSP client -DelegateComputer "hv-99.domain.local" -Force
New-PSSession -ComputerName "hv-99.domain.local" | Enter-PSSession
Invoke-Command -ComputerName "hv-99.domain.local" -Scriptblock {Enable-WSManCredSSP Server -Force}
Invoke-Command -ComputerName "hv-99.domain.local" -Scriptblock {Exit-PSSession}
Remove-PSSession -ComputerName "hv-99.domain.local"
New-PSSession "hv-99.domain.local" -Authentication Credssp -Credential alpha\administrator | Enter-PSSession
Invoke-Command -FilePath \\fileserver\share$\HDDinfo.ps1
Invoke-Command -ScriptBlock {Disable-WSManCredSSP -Role Server}
Invoke-Command -ScriptBlock {Exit-PSSession}
Remove-PSSession -ComputerName "hv-99.domain.local"
And the content of HDDinfo.ps1:
New-PSDrive -Name hvcheck -Root \\server\share$\HYPERVCHECK\ -PSProvider FileSystem
$servernaam = HOSTNAME
$servernaam = "HOSTNAME:" + $servernaam
write-output $servernaam | out-file <#-width 120#> hvcheck:\HDDINFO.txt -Encoding UTF8
$dag = get-date -format u
$dag = "DAG:" + $dag
write-output $dag | out-file <#-width 120#> -append hvcheck:\HDDINFO.txt -Encoding UTF8
Get-Volume | select-object driveletter, healthstatus, size, sizeremaining, drivetype,Filesystemlabel | format-table -autosize | out-file <#-width 120#> -append hvcheck:\HDDINFO.txt -Encoding UTF8
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works perfect! Thanks
Can I ask one last question?
Is there an easy way to make this script run for multiple servers?
If, for example, I make a txt file with all the server names, each on it's own line, is it possible to fill the $computer variable with those server names?
Can I ask one last question?
Is there an easy way to make this script run for multiple servers?
If, for example, I make a txt file with all the server names, each on it's own line, is it possible to fill the $computer variable with those server names?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many many thanks!
ASKER
However, I get following error:
Open in new window
Does it have to do something with "Invoke-Command -FilePath \\fileserver\share$\HDDinf