Restarting an app pool using powershell Exception - powershell

I am trying to restart an application pool remotely using powershell.
net use $ToPath $pass /USER:$usr
$appPool = get-wmiobject -computername $ToServerName -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"} -Authentication PacketPrivacy
#(Get-WmiObject -Query "SELECT * FROM IIsApplicationPool WHERE Name = 'W3SVC/AppPools/$appPoolName'" -Namespace 'root\MicrosoftIISv2').Recycle()
$appPool.Recycle()
net use $ToPath /delete
I basically use the same command that I use to move files remotely, where I set up a net user. I get a Get-WMI exception

I wanted to make sure that this question was answered for those that come after me. It turns out that I was piping the -Authentication PacketPrivacy parameter to the wrong command

Related

VSCode Running PowerShell script VS each command separately produces different results

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:
PC: My pc (WIN 10 is in one domain (Home)
VM: My remote lab machine (WIN 2016 is in another domain (Work)
PC: To get remote access to my lab I need to use VPN app
PC: My WSMan Trusted List = *
VM: WinRM is running in my remote lab (GPO: Windows Remote Management (WS-Management (Startup Mode = Automatic)
VM: port 5985 is open, 5986 not
PC & VM: Set-ExecutionPolicy Unrestricted in client and server wasn't help
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?
Finally problem solved: l add the domain name as a suffix in the DNS suffixes list in the Network adapter > IPv4 properties

finding server uptime from another server using powershell

I want to find the server up time of other servers using powershell command from one server. I am using below command to query the other server but could not get the required result.
$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime
Write-Host $lastboottime
Can someone share the best way to find the other servers uptime. Is there any way in sqlserver or sqlserver stored procedure
You need to convert it to a valid Datetime object, use the ConvertToDateTime method...
$WMI = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName $server -Credential $altcreds -ErrorAction SilentlyContinue)
[datetime]::Now - ($WMI.ConvertToDateTime($WMI.LastBootUpTime))
If you just need the date:
$WMI.ConvertToDateTime($WMI.LastBootUpTime)

Invoking a deployed SCCM package through powershell

I've been struggling to figure out a way to invoke a package that's already deployed out to a client in SCCM, using Powershell.
I'm able to run a Application using WMI, but it only works with applications, not packages.
$Application = (Get-CimInstance -ClassName CCM_Application -Namespace "root\ccm\clientSDK" -ComputerName $Computername | Where-Object {$_.Name -like $AppName})
$Args = #{EnforcePreference = [UINT32] 0
Id = "$($Application.id)"
IsMachineTarget = $Application.IsMachineTarget
IsRebootIfNeeded = $False
Priority = 'High'
Revision = "$($Application.Revision)" }
Invoke-CimMethod -Namespace "root\ccm\clientSDK" -ClassName CCM_Application -ComputerName $Computername -MethodName $Method -Arguments $Args
Anyways, using the CCM_Application class, i'm able to list out all of the applications deployed to that workstation, and I can install/uninstall, it just doesn't show any packages. I found the CCM_Program / CCM_ProgramManager class, but it doesn't return any packages/task sequences. Has anyone else been able to achieve this? or is it not possible through Powershell? I found some .dll that someone wrote in C# which seems to work with applications/packages, but I'd like to not load any dependencies if possible.

How to find the version of Trend from the Registry of a specific PC?

I was wondering if there is a way to find a registry value of a specific computer. The only way I could find is entering a pssession and then exiting.
$Computer = Read-Host "Enter the PC Name: "
$connection=test-connection -ComputerName $Computer -Quiet
if($connection -eq $True) {
Enter-PSSession $Computer
$TrendServer= Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TrendMicro\PC-cillinNTCorp \CurrentVersion | Select Server
write-output $TrendServer
if($TrendServer -ne $null){
Exit-PSSession
}
} else{Write-Output "Computer is not available. Please check Lan Sweeper "}
If it is installed using Windows Installer, you can use WMI, though this class is known to be quite slow:
Get-CimInstance -Query "SELECT * FROM Win32_Product WHERE Name = 'TrendMicro'" `
-ComputerName $computer
Change the name from 'TrendMicro' to whatever it actually is (I don't have it installed to check), and for older versions of PowerShell, use Get-WmiObject instead of Get-CimInstance.
Get more information here: Working with Software Installations

Restart-service WinRM remotely without yes/no

How can I Restart-service remotely with WinRM, without a yes/no confirmation on a remote computer in a workgroup?
You can use WMI:
$ServiceName = 'Spooler'
$WMI = Get-WmiObject win32_service -Filter "name='$ServiceName'" -ComputerName $computer
$WMI.StopService()
$WMI.StartService()