Getting Strange errors from Server - PowerShell - powershell

When I try to run this simple line of code on one of our Print Servers it fails, but it works fine on the other Server.
get-wmiobject -Class Win32_Printer -ComputerName "PRINT-SERVER" | Select-Object Name, Direct >>C:\Temp\Printer-Direct.txt
I get the following:
At line:1 char:1
get-wmiobject -Class Win32_Printer -ComputerName "PRINT-SERVER" | S ...
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Any ideas?
BTW - the name is correct, the server responds to Test-NetConnection and my brain is in a fog today thanks to Daylight Saving.

The issue wasn't with PowerShell or anything to do with the code. The error I was getting was a direct issue with the Print Server.
I didn't do any diagnostics since rebooting the server solved the problem. If the issue appears again, I may have to run the WMI Diagnostic tools to find out what is the problem, but I suspect it might have been a pending update on the Server due to Security updates.
Thank you to AussieJoe for the tip.

Related

Error calling get-wmiobject for Win32_powerplan

I'm trying to return the Win32_PowerPlan in PowerShell 7 on Windows 11 with the following code:
get-wmiobject -namespace "root\cimv2\power" -class Win32_powerplan
However, I get the output:
get-wmiobject :
At line:1 char:1
+ get-wmiobject -namespace "root\cimv2\power" -class Win32_powerplan
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Not sure what could be going on or how to get better error logs. The official docs use this exact code snippet, so not sure what it should change to if it's wrong.
Potentially related to PowerShell call to Win32_PowerPlan showing invalid class error
It would appear that, to retrieve the information you are seeking, PowerShell needs to be run in "elevated" mode (i.e., "Run as Administrator").
When doing so, I see the information as intended; however, when running 'normally' (even though I'm logged on with admin rights), I see the same error message that you have reported.

Why does Invoke-Command { net localgroup } fail?

I can successfully use NET USE and NET USER commands on the remote machine. Why not NET LOCALGROUP?
PS C:\src\powershell> Invoke-Command -ComputerName OTHERMACHINE -ScriptBlock { & NET LOCALGROUP }
System error 1312 has occurred.
+ CategoryInfo : NotSpecified: (System error 1312 has occurred.:String) [], Rem
oteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : OTHERMACHINE
A specified logon session does not exist. It may already have been terminated.
Passing the /USER parameter produces a different error.
PS C:\src\powershell> Invoke-Command -ComputerName OTHERMACHINE -ScriptBlock { & net localgroup /USER:THEDOM\theuser }
The option /USER:THEDOM\theuser is unknown.
+ CategoryInfo : NotSpecified: (The option /USE...son is unknown.:String) [], R
emoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : OTHERMACHINE
The syntax of this command is:
NET LOCALGROUP
[groupname [/COMMENT:"text"]] [/DOMAIN]
groupname {/ADD [/COMMENT:"text"] | /DELETE} [/DOMAIN]
groupname name [...] {/ADD | /DELETE} [/DOMAIN]
More help is available by typing NET HELPMSG 3506.
I believe this is the double-hop problem. You get the same error if you try to execute net localgroup administrators in a session created with Enter-PSSession: "System error 1312 has occurred." and "A specified logon session does not exist. It may already have been terminated."
net.exe is trying to reauthenticate, but it can't reuse the credentials of the session. You may have success using CredSSP, which requires some setup and, IIRC, may have security implications. You'll have to enable CredSSP on the remote system and local system, and then delegate correctly. Even then I'm seeing conflicting reports about this working. There may even be security policies preventing it.
Personally, I just wouldn't use net.exe at all. That's too many hoops to jump through for something so basic. You can retrieve the members of a local group remotely via the ADSI provider, which is probably much easier and much more likely to work.

MSBTS_HostSetting is Not Found

Upon building a script to automate the deletion of Host and Host Instances, I run the script below in PowerShell.
PS
C:\WINDOWS\system32>
[System.Management.ManagementObject]$objHostSetting =
([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").Delete()
However, after running the script, it seems that the MSBTS_HostSetting is gone because I was receiving the error below every time I am running it.
PS
C:\WINDOWS\system32>
[System.Management.ManagementObject]$objHostSetting
=[WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting" Cannot convert value "root/MicrosoftBizTalkServer:MSBTS_HostSetting" to type
"System.Management.ManagementClass". Error: "Not found " At line:1
char:2
+ [System.Management.ManagementObject]$objHostSetting =[WmiClass]"root/MicrosoftB ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToWMIClass
I am trying to load the BizTalkOMExplorer but the MSBTS_HostSetting has not returned. Any suggestions or knowledge how can I bring it back.
Just fixed my issue! Below are what I've done.
Open CMD and register the BTSWMIProvider.dll
Example:
Regsvr32.exe “C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\Bins32\BTSWMIProvider.dll“
Run the following in CMD as well.
mofcomp.exe BTSWMISchema.mof
mofcomp.exe BTSWMISchema.mfl
Restart WMI in services.
This would do the trick! :)
With PowerShell, you can get your HostInstances as:
$hostInstances = Get-WmiObject MSBTS_HostInstance -namespace root\MicrosoftBizTalkServer -ErrorAction Stop
And then you can find which one you want to delete and invoke Delete method:
$hostInstances[0].Delete()
The same with Hosts:
$hosts = Get-WmiObject MSBTS_Host -Namespace root\MicrosoftBizTalkServer -ErrorAction Stop
$hostToDelete = $hosts | where {$_.Name -eq 'HostNameToDelete'}
$hostToDelete.Delete()

Error 'Get-WmiObject : Invalid class "Msvm_ImageManagementService"'

Recently I've been getting this error, and I have no idea what triggered it. The documentation I found regarding this class is minimal and I'm not sure what I need to check or where to look in order to understand it and fix it.
System:
Windows Server 2012 R2 Datacenter Preview Build 9431
PowerShell Version 4.0 32bit and 64bit
Output:
PS > Get-WmiObject -Class "Msvm_ImageManagementService" -Namespace "root\virtualization"
Get-WmiObject : Invalid class "Msvm_ImageManagementService"
At line:1 char:1
+ Get-WmiObject -Class "Msvm_ImageManagementService" -Namespace "root\virtualizati ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
The class is still there, it now resides in a different namespace. It's in the namespace "root\virtualization\v2". It's also worth noting that the class has changed, previously used methods may not exist anymore.

Powershell stop-service error: cannot find any service with service name

I'm working on a script to deploy vendor software to a large environment. The first step is to stop the services in question. The script executes fine in our test environment, but I'm not an admin in the production environment so I'm convinced it's a permissions issue. I can't get admin rights to the prod environment so I need to try to find out anything that I may need to set to grant permissions to stop services remotely. I'm issuing the following command to stop services:
Stop-Service -InputObject $(Get-Service -Computer $destination.Server -Name ("moca."+$destEnv))
When I run the script I get:
Cannot find any service with service name 'moca.WMSPRD'.
+ CategoryInfo : ObjectNotFound: (moca.WMSPRD:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand
The service definitely exists and if I rdp into the target box and issue the stop-service command locally it will execute. So there is something preventing me from stopping the service remotely. Any ideas?
Edit:
A coworker suggested using WMI so tried replacing the Stop-Service line with:
(Get-WmiObject -computer $destination.Server Win32_Service -Filter ("Name='moca."+$destEnv+"'")).InvokeMethod("StopService",$null)
and I get:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
If you know the exact service name you can try this
(Get-WmiObject -computerName $_.name Win32_Service -Filter "Name='moca'").StopService()
Here im assuming that the service name is moca
Is DCOM working on the remote computer? I know how to do it with remote powershell, which uses wsman:
invoke-command comp001 { stop-service adobearmservice }