MSBTS_HostSetting is Not Found - powershell

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()

Related

PowerShell Stopping and Starting Services with Server names masked by DNS Alias

I have a simple PowerShell that accepts a file as a parameter that contains Server-Name, Service-Name and Timeout specified in seconds and the powershell stops/starts the services based on the command. I have 10 servers where application related services are configured and I'm using Workload automation capabilities to run this code on any server with the same parameter file and powershell present on all 10 servers.
All of this works fine with server names hardcoded in the param file. We have n+2 Infrastructure, and I created DNS Aliases for all the 10 servers to make Active-Standby switchover little easy. Now the problem comes with stop/start script. Ex: If the script gets invoked in Server 7, all the services will be shutdown except for the ones in Server 7 and I get below error:
Get-Service : Cannot find any service with service name
'Application-Server7'. At line:1 char:1
+ Get-Service -ComputerName app7test -Name Application-Server7
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Application-Server7:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
I just tested one-liner command using PowerShell ISE on the computer "appnewora79" and below are the results.
This works fine:
Get-Service -ComputerName appnewora79 -Name Application-Server7
Status Name DisplayName
------ ---- -----------
Stopped Application-Ser... Application-Server7
This fails:
Get-Service -ComputerName app7test -Name Application-Server7
Get-Service : Cannot find any service with service name 'Application-Server7'
At line:1 char:1
+ Get-Service -ComputerName app7test -Name Application-Server7
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Application-Server7:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Any suggestions to overcome this, please?
I tried below as well:
It all works when the server name is hardcoded. If I change it to DNS Alias name then Get-Service fails on the server where it is executing while fetching service details only related to the same server.
On the Server-1, if I run below commands, they all work fine
Get-Service -ComputerName servername1
Get-Service -ComputerName servername2
Get-Service -ComputerName servername3
If I update the above command with server dns alias and execute them on Server-1, then only the first command fails, i.e.. the command with the same server name masked with DNS fails, If I run the same commands on server 2, then the second one alone fails
Get-Service -ComputerName dnsalias1
Get-Service -ComputerName dnsalias2
Get-Service -ComputerName dnsalias3
I tried, test-connection with DNS alias names and they go thru fine. Tried Get-WmiObject -Class WIN32_service with computer name masked as DNS alias and it goes thru fine. I'm lost as to what could be the issue.
Below is the error message with Get-Service alone:
Get-Service : Cannot open Service Control Manager on computer 'dnsalias1'. This operation might require other privileges.
At line:5 char:1
+ Get-Service -ComputerName dnsalias1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Service], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetServiceCommand
I found a kind of different way to resolve this as per suggestion received in PowerShell Google+ forum.
I used Resolve-DnsName to get hold of IP4Address of the server and then passed it for stopping and starting windows services and this works.

PS4, Get-WMIObject returns PID=0 for an in-house developed service

Thanks in advance.
Here is a Powershell4 script that we're running:
$process = Get-WMiObject Win32_Service -Filter "Name ='HotKeyService'"
write-host Process Name = $process.name
write-host Process ID = $process.processid
$oopid = $process.processid
stop-process -id $oopid -force
wait-process -id $oopid -timeout 60 -WarningAction SilentlyContinue
-------------------------------
working directory: C:\Program Files\HK\HK.HotKeyService
With this I get:
Process Name = HotKeyService
Process ID = 0
Error#1
stop-process : Cannot stop process "Idle (0)" because of the following
error: Access is denied
At C:\Windows\TEMP\tmp206027652026805712.ps1:6 char:1
+ stop-process -id $oopid -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.Diagnostics.Process (Idle):P
rocess) [Stop-Process], ProcessCommandException
+ FullyQualifiedErrorId : CouldNotStopProcess,Microsoft.PowerShell.Command
s.StopProcessCommand
Error#2
wait-process : This command stopped operation because it cannot wait
on
'System Idle' process. Specify another process and Run your command again.
At C:\Windows\TEMP\tmp206027652026805712.ps1:7 char:1
+ wait-process -id $oopid -timeout 60 -WarningAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Diagnostics.Process
(Idl
e):Process) [Wait-Process], ProcessCommandException
+ FullyQualifiedErrorId :
WaitOnIdleProcess,Microsoft.PowerShell.Commands.
WaitProcessCommand
===============================
command exit code: 0
The code above is part of a larger process that gets run on many services on Windows 2012 servers and Win 7 workstations. Sometimes though it fails and I don't know why. In this case, the correct service name is retrieved which can be seen in the command ouptut but the PID is 0, which can't possibly be correct as that's the system idle process's PID.
The parent software calling this PS script later shows that files in the HotKey install folder cannot be updated because, "(The process cannot access the file because it is being used by another process)", which makes sense because the HotKeyService service didn't stop correctly.
I ran the parent software again and it worked correctly on the same service.
My question is how could Get-WMIObject return the correct service name but then on the next step show a PID of zero? I can't figure this one out.
Thanks for the assistance.
KJ
When you see a Win32_Service instance with a ProcessId value of 0, it simply means that the service isn't running.
You'll find that:
(Get-WMiObject Win32_Service -Filter "Name ='HotKeyService'").State
is Stopped

Powershell null pointer exception

I've been working on and off on a project to compress and move files older than x days to an archival folder. I've retrofitted the script here: https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Compress-Log-121e63b5 to assist me however I'm running into an issue that has proved fairly annoying.
When I run this on my machine, utilizing local directories, the script completes as expected. However, when I pass networked file paths to the script, the Get-WmiObject query begins returning null results.
For example, this is a sample command line that works:
powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 c:\temp\ c:\temp\compress_test\ 14
When I move to a UNC path, I begin getting the null-valued expression error on the WMIQuery.Compress() call
powershell -executionpolicy remotesigned -File compress_and_move_files.ps1 \\server1\temp\ \\server1\temp\compress_test\ 14
This is the full error:
You cannot call a method on a null-valued expression.
At compress_and_move_files.ps1:14 char:23
+ If ($WMIQuery.Compress <<<< ()) {Write-Host "$FullName compressed successfull
y."-ForegroundColor Green}
+ CategoryInfo : InvalidOperation: (Compress:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
That script attempts to retrieve a CIM_DataFile instance - a class that isn't accessible via UNC paths in WMI.
Change the script to target the remote computer and then use the local file system path:
$Server = "server1"
$WMIFile = "C:\temp\".Replace("\", "\\")
$WMIQuery = Get-WmiObject -Computer $Server -Query "SELECT * FROM CIM_DataFile WHERE Name='$WMIFileName'"

Unable to execute DSC file i n Windows server 2012 R2 from remote machine

When I execute the command to machine I get the following error -
PS C:\Windows\system32> $cimsession = New-CimSession -Credential (Get-Credential -UserName "test" -Message "test") -ComputerName test.cloudapp.net -Port 58718 -SessionOption $cimsessionoption
PS C:\Windows\system32> Get-DscConfiguration -CimSession $cimsession
Get-DscConfiguration : Current configuration does not exist. Execute Start-DscConfiguration command with -Path parameter to specify a
configuration file and create a current configuration first.
At line:1 char:1
+ Get-DscConfiguration -CimSession $cimsession
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_DSCLocalConfigurationManager:root/Microsoft/...gurationManager) [Get-DscConfiguration],
CimException
+ FullyQualifiedErrorId : MI RESULT 1,Get-DscConfiguration
+ PSComputerName : powerlabdns.cloudapp.net
What does this mean?
You are not executing a DSC file, you are requesting the current DSC configuration which does not exist (as the error says).
You need to run Start-DscConfiguration first, again as the error says to. You must pass a -Path to that call which is a directory (relative to the target node) where an MOF file (the compiled configuration) resides.
Once you do that, you'll be able to call Get-DscConfiguration to see the current config.

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 }