Entering in commands on a remote workstation using a PS script
I am trying to create a PS script that opens a session on a remote workstation and executes these commands:
$session = New-PSSession -ComputerName $workstationName
# Prompt for credentials
$cred = Get-Credential
# Use Invoke-Command to run the script block with elevated credentials
Invoke-Command -Session $session -Credential $cred -ScriptBlock {
# Check if the session is available
if ($session -ne $null) {
Write-Host “Session established. Waiting for the session to be ready…”
# PowerShell commands here
netsh advfirewall firewall set rule name=”File and Printer Sharing (Echo Request – ICMPv4-In)” new enable=yes profile=domain
Set-ItemProperty -Path ‘HKLM:SystemCurrentControlSetControlTerminal Server’-name “fDenyTSConnections” -Value 0
Enable-NetFirewallRule -DisplayGroup “Remote Desktop”
netsh advfirewall firewall set rule group=”windows management instrumentation (wmi)” new enable=yes
Set-NetFirewallRule -DisplayGroup “Network Discovery” -Enabled True
Get-Process
}
# Exit the remote session
Exit-PSSession
} else {
Write-Host “Failed to establish a session.”
}
Here is the error. Any help is appreciated
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At C:XXXXVMPrepBeta6.ps1:67 char:5
+ Invoke-Command -Session $session -Credential $cred -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
I am trying to create a PS script that opens a session on a remote workstation and executes these commands: netsh advfirewall firewall set rule name=”File and Printer Sharing (Echo Request – ICMPv4-In)” new enable=yes profile=domain Set-ItemProperty -Path ‘HKLM:SystemCurrentControlSetControlTerminal Server’-name “fDenyTSConnections” -Value 0 Enable-NetFirewallRule -DisplayGroup “Remote Desktop” netsh advfirewall firewall set rule group=”windows management instrumentation (wmi)” new enable=yes Set-NetFirewallRule -DisplayGroup “Network Discovery” -Enabled True This is the part of the script giving me trouble: $session = New-PSSession -ComputerName $workstationName# Prompt for credentials$cred = Get-Credential# Use Invoke-Command to run the script block with elevated credentialsInvoke-Command -Session $session -Credential $cred -ScriptBlock {# Check if the session is availableif ($session -ne $null) {Write-Host “Session established. Waiting for the session to be ready…”# PowerShell commands herenetsh advfirewall firewall set rule name=”File and Printer Sharing (Echo Request – ICMPv4-In)” new enable=yes profile=domainSet-ItemProperty -Path ‘HKLM:SystemCurrentControlSetControlTerminal Server’-name “fDenyTSConnections” -Value 0Enable-NetFirewallRule -DisplayGroup “Remote Desktop”netsh advfirewall firewall set rule group=”windows management instrumentation (wmi)” new enable=yesSet-NetFirewallRule -DisplayGroup “Network Discovery” -Enabled TrueGet-Process}# Exit the remote sessionExit-PSSession} else {Write-Host “Failed to establish a session.”} Here is the error. Any help is appreciated Invoke-Command : Parameter set cannot be resolved using the specified named parameters.At C:XXXXVMPrepBeta6.ps1:67 char:5+ Invoke-Command -Session $session -Credential $cred -ScriptBlock {+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand Read More