0
votes

Starting to manage servers with PowerShell scripts...

I installed VSCODE + PowerShell extension in my laptop.

I set my WSMAN Trusted list to *

And ran the following script (below, for example with F5 key) that suppose to stop a service in the remote machine:

$computers = "IP ADDRESS"
[string] $domainAdminUserName = "USERNAME"
[string] $domainAdminPlainPassword = "PASSWORD"
$securePassword = $domainAdminPlainPassword | ConvertTo-SecureString -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $domainAdminUserName, $securePasswordEnter-PSSession -Computer $computers -Credential $credentials
    
$svcSQL = Get-WmiObject -Class Win32_Service -Computer $computers -Credential $credentials -Filter "Name='MSSQLSERVER'"
$svcSQLAgent = Get-WmiObject -Class Win32_Service -Computer $computers -Credential $credentials -Filter "Name='SQLSERVERAGENT'"
$svcOLAP = Get-WmiObject -Class Win32_Service -Computer $computers -Credential $credentials -Filter "Name='MSSQLServerOLAPService'"
    
Invoke-Command -Computer $computers -Credential $credentials -ScriptBlock {Get-WmiObject -Class Win32_Service | Where-Object {$_.name -eq $svcSQL} | Stop-Service -Force}
Invoke-Command -Computer $computers -Credential $credentials -ScriptBlock {Get-WmiObject -Class Win32_Service | Where-Object {$_.name -eq $svcOLAP} | Stop-Service}
    
Exit-PSSession

The services weren't stop

Then, I went to the PowerShell Integrated Console and execute the following commands:

PS> Enter-PSSession -Computer "IP ADDRESS" -Credential "DOMAIN ADMIN USER"
PS> PASSWORD: ******
PS [IP ADDRESS]> Get-Service -Name "MSSQLSERVER"
PS [IP ADDRESS]> PowerShell found the service
PS [IP ADDRESS]> Stop-Service -Name "MSSQLSERVER"

The service was stopped successfully

How can I fix the problem in order to run the script and get the expected result?


Note:

  1. PC: My pc (WIN 10 is in one domain (Home)
  2. VM: My remote lab machine (WIN 2016 is in another domain (Work)
  3. PC: To get remote access to my lab I need to use VPN app
  4. PC: My WSMan Trusted List = *
  5. VM: WinRM is running in my remote lab (GPO: Windows Remote Management (WS-Management (Startup Mode = Automatic)
  6. VM: port 5985 is open, 5986 not
  7. PC & VM: Set-ExecutionPolicy Unrestricted in client and server wasn't help
2
Remove [ValidateNotNullOrEmpty()]. These are meant to be used with parameters, not for simply declaring variables in the code. If parameters is what you want, put the first three lines inside a Param(..) blockTheo
@Theo. thanks for the comment. fixed the code block and its work without errors.Hiddai
Glad to hear that. Well done!Theo
@Theo - Now for the main issue - Do you have any clue how to solve this issue? Is there any configuration need to do with VSCODE to make it work as with the usual Windows PowerShell?Hiddai
I guess the Powershell editor is more forgiving than VsCode. I don't think you should try to make any effort in VsCode accepting code that does not follow syntax rules. Just use [Validate*] on parameters, not in variable creation.Theo

2 Answers

0
votes

I'm just learning but had a similar issue that was resolved when I added this:

PS>  Set-ExecutionPolicy Unrestricted

Visual studio code terminal, how to run a command with administrator rights?

0
votes

Finally problem solved: l add the domain name as a suffix in the DNS suffixes list in the Network adapter > IPv4 properties