Link to home
Start Free TrialLog in
Avatar of Itxx
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$\HDDresult.txt' is denied.


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"

Open in new window


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

Open in new window

SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Itxx
Itxx

ASKER

Thank you!

However, I get following error:

Parameter set cannot be resolved using the specified named parameters.
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
    + PSComputerName        : hv-99.alpha.local

Open in new window


Does it have to do something with "Invoke-Command -FilePath \\fileserver\share$\HDDinfo.ps1" ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Itxx

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Itxx

ASKER

Many many thanks!