Forum Discussion
andrewtinaza
Oct 31, 2024Copper Contributor
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: netsh advfirewall firewall set rule name="File and Printer Sharing (Echo Request - ICM...
LainRobertson
Oct 31, 2024Silver Contributor
Hi, Andrew.
You're getting that error because using both -Session and -Credential on the same call to Invoke-Command is not a supported combination, which you can see from the documentation:
That said, this script is also unnecessarily complex, where you can simplify it down to the following:
# Prompt for credentials
$cred = Get-Credential;
# Use Invoke-Command to run the script block with elevated credentials
Invoke-Command -ComputerName $workstationName -Credential $cred -ScriptBlock {
# PowerShell commands here
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0;
Enable-NetFirewallRule -Name "FPS-ICMP4-ERQ-In-NoScope"; # Using the pre-defined name of the rule specific to the domain profile.
Enable-NetFirewallRule -DisplayGroup "Remote Desktop", "Network Discovery", "windows management instrumentation (wmi)";
Get-Process;
}
The only time you might look to create a separate session would be if you have a valid use case for calling Invoke-Command against the same workstation multiple times, but I can't recall seeing such a scenario.
Cheers,
Lain