PowerShell BizTalk scripts WITHOUT using BizTalk provider for PowerShell - powershell

I have been using the excellent BizTalk Provider for PowerShell from Codeplex for a while now. However in my new company the build team are not comfortable using it so I need to rewrite all my handy configuration scripts to not use it.
I have re-done most of them but am having some problems finding examples of how to do the following two things:
Create host instances
Create send/receive adapter handers
Can anyone point me to examples of how to do these things WITHOUT the PowerShell provider for BizTalk please? I have done some looking and it seems like everyone uses this now.
Many thanks in advance.

OK here are my finished hacky powershell functions if anyone else needs them:
function write-WarnMessage([string]$message) {
Write-Host $(Get-Date) $message -ForegroundColor Yellow
}
function write-SucessMessage([string]$message) {
Write-Host $(Get-Date) $message -ForegroundColor Green
}
function write-InfoMessage([string]$message) {
Write-Host $(Get-Date) $message -ForegroundColor Blue -BackgroundColor White
}
function write-ErrorMessage ([string]$message) {
Write-Host $(Get-Date) $message -ForegroundColor Red
}
# Gets the execution directory
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
function Delete-Bts-Receive-Handler ( [string]$adapter, [string]$hostName )
{
try
{
[System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_ReceiveHandler' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapter'"
$objHandler.Delete()
write-SucessMessage "Deleted $adapter receive handler for $hostName"
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message -eq "You cannot call a method on a null-valued expression.")
{
write-WarnMessage "$adapter receive handler for $hostName does not exist"
}
elseif ($_.Exception.Message.IndexOf("Cannot delete a receive handler that is used by") -ne -1)
{
write-WarnMessage "$adapter receive handler for $hostName is in use. Cannot delete."
}
else
{
write-Error "$adapter receive handler for $hostName could not be deleted: $_.Exception.ToString()"
}
}
}
function Delete-Bts-Send-Handler ( [string]$adapter, [string]$hostName )
{
try
{
[System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_SendHandler2' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapter'"
$objHandler.Delete()
write-SucessMessage "Deleted $adapter send handler for $hostName"
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message -eq "You cannot call a method on a null-valued expression.")
{
write-WarnMessage "$adapter send handler for $hostName does not exist"
}
elseif ($_.Exception.Message.IndexOf("Cannot delete a send handler that is used by") -ne -1)
{
write-WarnMessage "$adapter send handler for $hostName is in use. Cannot delete."
}
else
{
write-Error "$adapter send handler for $hostName could not be deleted: $_.Exception.ToString()"
}
}
}
function Delete-Bts-Instance( [string]$hostName, [string]$Server )
{
try
{
# Unintall
[System.Management.ManagementObject]$objHostInstance = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostInstance").CreateInstance()
$name = "Microsoft BizTalk Server " + $hostName + " " + $Server
$objHostInstance["Name"] = $name
$objHostInstance.Uninstall()
# Unmap
[System.Management.ManagementObject]$objServerHost = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ServerHost").CreateInstance()
$objServerHost["HostName"] = $hostName
$objServerHost["ServerName"] = $Server
$objServerHost.Unmap()
write-SucessMessage "Deleted host instance for $hostName on $Server"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host instance on server $Server could not be deleted: $_.Exception.ToString()"
}
}
function Stop-Bts-HostInstance ( [string] $HostName, [string] $Server )
{
try
{
$filter = "HostName = '" + $HostName + "' and RunningServer = '" + $Server + "'"
$HostInstance = get-wmiobject "MSBTS_HostInstance" -namespace "root\MicrosoftBizTalkServer" -filter $filter
$HostInstanceState = $HostInstance.GetState().State
write-InfoMessage "Current state of $HostName instance on server $Server : $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)"
if ($HostInstanceState -eq 4)
{
$HostInstance.Stop()
$HostInstanceState = $HostInstance.GetState().State
write-SucessMessage "New state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)"
}
else
{
$HostInstanceState = $HostInstance.GetState().State
write-WarnMessage "Failed to stop host instance $HostName on server $Server because host instance state $HostInstanceState was not the expected value of 4 (running)"
}
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host instance could not be stopped on $Server : $_.Exception.ToString()"
}
}
function Delete-Bts-Host ( [string]$hostName )
{
# TODO: This only works intermittently
try
{
[System.Management.ManagementObject]$objHostSetting = get-wmiobject 'MSBTS_HostSetting' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName'"
$objHostSetting.Delete()
write-SucessMessage "Deleted host $hostName"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host could not be deleted: $_.Exception.ToString()"
}
}
# function to create BizTalk host
function Create-Bts-Host(
[string]$hostName,
[int]$hostType,
[string]$ntGroupName,
[bool]$authTrusted,
[bool]$tracking,
[bool]$32BitOnly)
{
try
{
[System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
$objHostSetting["Name"] = $hostName
$objHostSetting["HostType"] = $hostType
$objHostSetting["NTGroupName"] = $NTGroupName
$objHostSetting["AuthTrusted"] = $authTrusted
$objHostSetting["IsHost32BitOnly"] = $32BitOnly
$objHostSetting["HostTracking"] = $tracking
$putOptions = new-Object System.Management.PutOptions
$putOptions.Type = [System.Management.PutType]::CreateOnly;
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = "System.Management"
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
[Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes)
$methodInfo.Invoke($objHostSetting, $putOptions)
write-SucessMessage "Host $hostName created"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host could not be created: $_.Exception.ToString()"
}
}
function Update-Bts-Host (
[string]$hostName,
[int]$hostType,
[string]$ntGroupName,
[bool]$authTrusted,
[bool]$tracking,
[bool]$32BitOnly)
{
try
{
[System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
$objHostSetting["Name"] = $hostName
$objHostSetting["HostType"] = $hostType
$objHostSetting["NTGroupName"] = $ntGroupName
$objHostSetting["AuthTrusted"] = $authTrusted
$objHostSetting["IsHost32BitOnly"] = $32BitOnly
$objHostSetting["HostTracking"] = $tracking
$putOptions = new-Object System.Management.PutOptions
$putOptions.Type = [System.Management.PutType]::UpdateOnly; # This tells WMI it's an update.
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = "System.Management"
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
[Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes)
$methodInfo.Invoke($objHostSetting, $putOptions)
write-SucessMessage "Host updated"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host could not be updated: $_.Exception.ToString()"
}
}
# function to create BizTalk send adapter handler
function Create-Bts-SendHandler([string]$adapter, [string]$hostName)
{
try
{
[System.Management.ManagementObject]$objSendHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_SendHandler2").CreateInstance()
$objSendHandler["AdapterName"] = $adapter
$objSendHandler["HostName"] = $hostName
$objSendHandler["IsDefault"] = $false
$putOptions = new-Object System.Management.PutOptions
$putOptions.Type = [System.Management.PutType]::CreateOnly;
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = "System.Management"
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objSendHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
[Reflection.MethodInfo] $methodInfo = $objSendHandlerType.GetMethod("Put", $targetTypes)
$methodInfo.Invoke($objSendHandler, $putOptions)
write-SucessMessage "Send handler created for $adapter / $hostName"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "Send handler for $adapter / $hostName could not be created: $_.Exception.ToString()"
}
}
# function to create BizTalk receive adapter handler
function Create-Bts-ReceiveHandler([string]$adapter, [string]$hostName)
{
try
{
[System.Management.ManagementObject]$objReceiveHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ReceiveHandler").CreateInstance()
$objReceiveHandler["AdapterName"] = $adapter
$objReceiveHandler["HostName"] = $hostName
$putOptions = new-Object System.Management.PutOptions
$putOptions.Type = [System.Management.PutType]::CreateOnly;
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = "System.Management"
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objReceiveHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
[Reflection.MethodInfo] $methodInfo = $objReceiveHandlerType.GetMethod("Put", $targetTypes)
$methodInfo.Invoke($objReceiveHandler, $putOptions)
write-SucessMessage "Receive handler created for $adapter / $hostName"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "Receive handler for $adapter / $hostName could not be created: $_.Exception.ToString()"
}
}
# function to create BizTalk host instance
function Create-Bts-Instance([string]$hostName, [string]$login, [string]$password, [string]$Server)
{
try
{
[System.Management.ManagementObject]$objServerHost = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ServerHost").CreateInstance()
$objServerHost["HostName"] = $hostName
$objServerHost["ServerName"] = $Server
$objServerHost.Map()
[System.Management.ManagementObject]$objHostInstance = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostInstance").CreateInstance()
$name = "Microsoft BizTalk Server " + $hostName + " " + $Server
$objHostInstance["Name"] = $name
$objHostInstance.Install($Login, $Password, $True)
write-SucessMessage "Created host instance for $hostName on $Server"
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host instance on server $Server could not be created: $_.Exception.ToString()"
}
}
function Start-Bts-HostInstance ( [string] $HostName, [string] $Server )
{
try
{
$filter = "HostName = '" + $HostName + "' and RunningServer = '" + $Server + "'"
$HostInstance = get-wmiobject "MSBTS_HostInstance" -namespace "root\MicrosoftBizTalkServer" -filter $filter
$HostInstanceState = $HostInstance.GetState().State
write-InfoMessage "Current state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)"
if ($HostInstanceState -eq 1)
{
$HostInstance.Start()
$HostInstanceState = $HostInstance.GetState().State
write-SucessMessage "New state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)"
}
else
{
$HostInstanceState = $HostInstance.GetState().State
write-WarnMessage "Failed to start host instance $HostName on server $Server because host instance state $HostInstanceState was not the expected value of 1 (stopped)"
}
}
catch [System.Management.Automation.RuntimeException]
{
write-Error "$hostName host instance could not be started on $Server : $_.Exception.ToString()"
}
}
function Install-BTSMsi ( [string]$bts_application, [string]$msi_package, [string]$install_env )
{
write-InfoMessage "Installing $msi_package in $bts_application for $install_env"
BTSTask ImportApp /ApplicationName:$bts_application /Package:$msi_package /Overwrite /Environment:$install_env
if ($LASTEXITCODE -ne 0) {
write-ErrorMessage "Failed to Import MSI $msi_package"
}
else
{
write-SucessMessage "Installed $bts_application for $install_env"
}
}
function Remove-BTSApplication ( [string]$appServer, [string]$appDatabase, [string]$appName )
{
write-InfoMessage "Uninstalling Application: $appName "
BTSTask RemoveApp /Server:"$appServer" /ApplicationName:"$appName" /Database:"$appDatabase" #| out-null
if ($LASTEXITCODE -ne 0) {
write-ErrorMessage "Failed to remove $appServer $appName"
}
else
{
write-SucessMessage "Removed $appName from $appServer"
}
}
# Accesses SSO and will require the build user account to belong to the SSO Admins group.
# Also requires Microsoft.BizTalk.ExplorerOM.dll to be loaded.
function StartStop-BTSApplication ( [string]$appServer, [string]$appName, [string]$appCommand )
{
if ( ($appName -eq '') -or ($appName -eq $null) )
{
throw 'you must supply the application name'
}
#write-InfoMessage " Finding Application: $appServer:$appName "
$exp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$exp.ConnectionString = Get-BTSConnectionString($appServer)
$app = $exp.Applications[$appName]
if($app -eq $null)
{
if ($appCommand -eq "stop" )
{
write-WarnMessage "WARNING failed to stop $appName"
}
else
{
write-WarnMessage "FAILED to start $appName"
}
}
else
{
switch -wildcard ( $app.Status.ToString() )
{
'Stopped'
{
if ($appCommand -eq "start" ) {
write-InfoMessage "Starting Application: $appName "
$null = $app.Start([Microsoft.BizTalk.ExplorerOM.ApplicationStartOption]::StartAll)
$null = $exp.SaveChanges()
write-SucessMessage " Started Application: $appName "
} else {
write-InfoMessage "Application Already Stopped: $appName "
}
}
'*Started'
{
# includes Started and PartiallyStarted
if ($appCommand -eq "stop" ) {
write-InfoMessage "Stopping Application: $appName "
$null = $app.Stop([Microsoft.BizTalk.ExplorerOM.ApplicationStopOption]::StopAll)
$null = $exp.SaveChanges()
write-SucessMessage " Stopped Application: $appName "
} else {
write-InfoMessage "Application Already Started : $appName "
}
}
'NotApplicable'
{
write-InfoMessage "Application doesn't require $appCommand"
}
default
{
$msg = "Unkown STATUS: " + $app.Status
write-ErrorMessage $msg
}
}
}
}
function Get-BTSConnectionString ( [string] $server )
{
$group = Get-WmiObject MSBTS_GroupSetting -n root\MicrosoftBizTalkServer -computername $server
$grpdb = $group.MgmtDBName
$grpsvr = $group.MgmtDBServerName
[System.String]::Concat("server=", $grpsvr, ";database=", $grpdb, ";Integrated Security=SSPI")
write-InfoMessage " Server: $grpsvr - Database $grpdb"
}

For 2) you should look into Microsoft.BizTalk.ExplorerOM
Using ReceiveHandler and SendHandler should give you what you need.
However there seems to be no way to control host instances, only hosts, using ExplorerOM.

Looks like you borrowed from Santhosh Benjamin's scripts to submit the PutOptions using reflection:
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = “System.Management”
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objHostSettingType = $sysMgmtAssembly.GetType(“System.Management.ManagementObject”)
[Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod(“Put”,$targetTypes)
$methodInfo.Invoke($objHostSetting,$putOptions)
I tested this with several scripts I am currently working at, and found out (through sheer stubbernness) that you can simply use the Put method on the object, just as you were used to in C#:
$objHostSetting.Put($putOptions)
Cheers,
Charles.

Even if you can't use the Powershell library itself, nothing is stopping you from examining its source to see what it does to perform those functions, which is to interact with the Microsoft.BizTalk.ExplorerOM assembly.

There is also a very useful blog by Sandro Pereira with functions for handling hosts, host instances and adapter handlers
And also Tomas Restrepo set on GitHub
The one they didn't seem to cover was deleting a BizTalk host ...
function Remove-BizTalkHost(
[string] $hostName)
{
try
{
$strQuery = "MSBTS_HostSetting.Name='$hostName'";
$objPath = New-Object System.Management.ManagementPath
$objPath.Path = $strQuery
[System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance()
$objHostSetting.Path = $objPath
$objHostSetting.Delete()
Write-Host "Host $hostName was successfully deleted" -ForegroundColor DarkGreen
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message.Contains("No instance was found with the specified key") -eq $true)
{
Write-Host "$hostName can't be deleted because already removed from the BizTalk group." -ForegroundColor DarkRed
}
else
{
Write-Error "$hostName host could not be deleted: $_.Exception.ToString()"
}
}
}

Related

CreateBizTalkAdapterHandler error

#############################################################
# This function will create a handler for a specific
# adapter on the new host, so these get used for processing.
# [direction]: 'Receive','Send'
#############################################################
function CreateBizTalkAdapterHandler(
[string]$adapterName,
[string]$direction,
[string]$hostName,
[string]$originalDefaulHostName,
[boolean]$isDefaultHandler)
#[boolean]$removeOriginalHostInstance)
{
if($direction -eq 'Receive')
{
[System.Management.ManagementObject]$objAdapterHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ReceiveHandler").CreateInstance()
$objAdapterHandler["AdapterName"] = $adapterName
$objAdapterHandler["HostName"] = $hostName
}
else
{
[System.Management.ManagementObject]$objAdapterHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_SendHandler2").CreateInstance()
$objAdapterHandler["AdapterName"] = $adapterName
$objAdapterHandler["HostName"] = $hostName
$objAdapterHandler["IsDefault"] = $isDefaultHandler
}
try
{
$putOptions = new-Object System.Management.PutOptions
$putOptions.Type = [System.Management.PutType]::CreateOnly;
[Type[]] $targetTypes = New-Object System.Type[] 1
$targetTypes[0] = $putOptions.GetType()
$sysMgmtAssemblyName = "System.Management"
$sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName)
$objAdapterHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject")
[Reflection.MethodInfo] $methodInfo = $objAdapterHandlerType.GetMethod("Put", $targetTypes)
$methodInfo.Invoke($objAdapterHandler, $putOptions)
Write-Host "$adapterName $direction Handler for $hostName was successfully created" -Fore DarkGreen
}
catch [System.Management.Automation.RuntimeException]
{
if ($_.Exception.Message.Contains("The specified BizTalk Host is already a receive handler for this adapter.") -eq $true)
{
Write-Host "$hostName is already a $direction Handler for $adapterName adapter." -Fore DarkRed
}
elseif($_.Exception.Message.Contains("The specified BizTalk Host is already a send handler for this adapter.") -eq $true)
{
Write-Host "$hostName is already a $direction Handler for $adapterName adapter." -Fore DarkRed
}
else {
write-Error "$adapterName $direction Handler for $hostName could not be created: $_.Exception.ToString()"
}
}
#if($removeOriginalHostInstance)
#{
#DeleteBizTalkAdapterHandler $adapterName $direction $originalDefaulHostName
#}
}
When I run
CreateBizTalkAdapterHandler -adapterName "WCF-WebHttp" -direction "Send" -hostName "ServiceProcessing" -originalDefaulHostName "BizTalkServerApplication" -isDefaultHandler 0
Is creating ServiceProcessing send handler but when I use
CreateBizTalkAdapterHandler -adapterName "WCF_WSHttp" -direction "Send" -hostName "PMProcessing" -originalDefaulHostName "BizTalkServerApplication" -isDefaultHandler 0
I'm getting error when I was dealing with "WCF-WSHttp" adapter:
CreateBizTalkAdapterHandler : WCF_WSHttp Send Handler for PMOServiceProcessing could not be created: Exception calling "Invoke" with
"2" argument(s): "No instance was found with the specified key. This could be the result of the instance being deleted by another
BizTalk Admin session.".Exception.ToString()
At line:3 char:1
+ CreateBizTalkAdapterHandler -adapterName "WCF_WSHttp" -direction "Sen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,CreateBizTalkAdapterHandler
CreateBizTalkAdapterHandler -adapterName "WCF-WSHttp" -direction "Send" -hostName "PMProcessing" -originalDefaulHostName "BizTalkServerApplication" -isDefaultHandler 0

Only prints the last server in the list, I want all servers

This only prints the last server in the list, I'm looking to get all servers and print to screen
$machines = (Get-BrokerMachine -AdminAddress $adminaddress -DesktopGroupName $deliverygroup | Select-Object DNSname).DNSname
foreach($machine in $machines){
$machinelist = Get-BrokerMachine -HostedMachineName $machine
if($machinelist.InMaintenanceMode -eq $true){
$status = "$machine is in maintenance mode"
}else {
$status = "$machine is not in maintenance mode"
}
}
Write-Host $status
Here is a more PowerShell-like approach (not tested):
Get-BrokerMachine -AdminAddress $adminaddress -DesktopGroupName $deliverygroup | ForEach-Object {
$machineName = $_.DNSName
[PSCustomObject] #{
"MachineName" = $machineName
"MaintenanceMode" = (Get-BrokerMachine -HostedMachineName $machine).InMaintenanceMode
}
} | Export-Csv "C:\whatever\results.csv" -NoTypeInformation
$Status is constantly being overwritten by the current machine in your list.
You're looking for:
$Status+=
As opposed to:
$Status=
You'll also want to explicitly state that $Status will be an array at the beginning like so:
$Status=#()
Or when you create the variable and omit the line at the beginning.
[array]$Status +=
Otherwise, you'll get results that run together as it will be treated as a [String]
another funky mode :
function get-BrokerMachineMode
{
param (
[Parameter(Mandatory = $true)]
[string[]]$machines
)
begin
{
$ErrorActionPreference = 'Stop'
Add-Type -Language CSharp #"
public class BrokenBroker {
qpublic System.String MachineName;
public System.String MaintenanceMode;
public BrokenBroker (string MachineName, string MaintenanceMode)
{
this.MachineName = MachineName;
this.MaintenanceMode = IsInMaintenanceMode;
}
}
"#
$status = #()
Write-Verbose "Created objects..."
}
process
{
try
{
$machines = (Get-BrokerMachine -AdminAddress $adminaddress `
-DesktopGroupName $deliverygroup | Select-Object DNSname).DNSname
foreach ($machine in $machines)
{
Write-Verbose "Checking machine: $machine"
$machinelist = Get-BrokerMachine -HostedMachineName $machine
if ($machinelist.InMaintenanceMode -eq $true)
{
$status += New-Object BrokenBroker($machine, $true)
}
else
{
$status += New-Object BrokenBroker($machine, $false)
}
}
}
catch
{
Write-Error $error[0].Exception.Message
}
$status
}
end
{
Write-Verbose "Done"
}
}
this is a function you just must to load then you can launch it just by using this command:
$computers = get-content = {PATH TO TXT FILE}
$list = get-BrokerMachineMode -machines $computers -Verbose

Powershell script to get certificate expiry for a website remotely for multiple servers

I am trying to create an script to get the certificate expiry date for an websites remotely for multiple servers. I have an script which is working for single server (Need to login into server and doing execution), I need to run this remotely for multiple servers. How can i modify this script to execute for multiple servers remotely. Please advice.
$servers = get-content D:\Certificate.txt
$DaysToExpiration = 60 #change this once it's working
$expirationDate = (Get-Date).AddDays($DaysToExpiration)
foreach ($server in $servers)
{
$sites = Get-Website | ? { $_.State -eq "Started" } | % { $_.Name }
$certs = Get-ChildItem IIS:SSLBindings | ? {
$sites -contains $_.Sites.Value
} | % { $_.Thumbprint }
Get-ChildItem CERT:LocalMachine/My | ? {
$certs -contains $_.Thumbprint -and $_.NotAfter -lt $expirationDate
}
}
Inspired by https://iamoffthebus.wordpress.com/2014/02/04/powershell-to-get-remote-websites-ssl-certificate-expiration/ I use following script:
$minimumCertAgeDays = 60
$timeoutMilliseconds = 10000
$urls = get-content .\check-urls.txt
#disabling the cert validation check. This is what makes this whole thing work with invalid certs...
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
foreach ($url in $urls)
{
Write-Host Checking $url -f Green
$req = [Net.HttpWebRequest]::Create($url)
$req.Timeout = $timeoutMilliseconds
$req.AllowAutoRedirect = $false
try {$req.GetResponse() |Out-Null} catch {Write-Host Exception while checking URL $url`: $_ -f Red}
$certExpiresOnString = $req.ServicePoint.Certificate.GetExpirationDateString()
#Write-Host "Certificate expires on (string): $certExpiresOnString"
[datetime]$expiration = [System.DateTime]::Parse($req.ServicePoint.Certificate.GetExpirationDateString())
#Write-Host "Certificate expires on (datetime): $expiration"
[int]$certExpiresIn = ($expiration - $(get-date)).Days
$certName = $req.ServicePoint.Certificate.GetName()
$certPublicKeyString = $req.ServicePoint.Certificate.GetPublicKeyString()
$certSerialNumber = $req.ServicePoint.Certificate.GetSerialNumberString()
$certThumbprint = $req.ServicePoint.Certificate.GetCertHashString()
$certEffectiveDate = $req.ServicePoint.Certificate.GetEffectiveDateString()
$certIssuer = $req.ServicePoint.Certificate.GetIssuerName()
if ($certExpiresIn -gt $minimumCertAgeDays)
{
Write-Host Cert for site $url expires in $certExpiresIn days [on $expiration] -f Green
}
else
{
Write-Host WARNING: Cert for site $url expires in $certExpiresIn days [on $expiration] -f Red
Write-Host Threshold is $minimumCertAgeDays days. Check details:`nCert name: $certName -f Red
Write-Host Cert public key: $certPublicKeyString -f Red
Write-Host Cert serial number: $certSerialNumber`nCert thumbprint: $certThumbprint`nCert effective date: $certEffectiveDate`nCert issuer: $certIssuer -f Red
}
Write-Host
rv req
rv expiration
rv certExpiresIn
}
Alternatively, you might find this advanced script useful:
you can switch between report output as Text, Html or PSObject
use the script with urls (parameter array) or with input file for urls or with pipeline input
improved stability: correctly handle missing certificates on HTTP connections
just put the code into a file like Check-ExpiringSslCerts.ps1
Here the advanced script code:
[CmdletBinding(DefaultParametersetname="URLs in text file")]
Param(
[ValidateSet('Text','Html','PSObject')]
[string]$ReportType = 'Text',
[int]$MinimumCertAgeDays = 60,
[int]$TimeoutMilliseconds = 10000,
[parameter(Mandatory=$false,ParameterSetName = "URLs in text file")]
[string]$UrlsFile = '.\check-urls.txt',
[parameter(Mandatory=$false,ParameterSetName = "List of URLs",
ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$Urls
)
Begin
{
[string[]]$allUrls = #()
$returnData = #()
[bool]$ProcessedInputPipeLineByArrayItem = $false
function CheckUrl ([string]$url, [array]$returnData)
{
[string]$details = $null
if ($ReportType -eq "Html")
{
$stringHtmlEncoded = [System.Web.HttpUtility]::HtmlEncode($url)
Write-Host "<tr><td>$stringHtmlEncoded</td>"
}
if ($ReportType -eq "Text") { Write-Host Checking $url }
$req = [Net.HttpWebRequest]::Create($url)
$req.Timeout = $timeoutMilliseconds
$req.AllowAutoRedirect = $false
try
{
$req.GetResponse() |Out-Null
if ($req.ServicePoint.Certificate -eq $null) {$details = "No certificate in use for connection"}
}
catch
{
$details = "Exception while checking URL $url`: $_ "
}
if ($details -eq $null -or $details -eq "")
{
$certExpiresOnString = $req.ServicePoint.Certificate.GetExpirationDateString()
#Write-Host "Certificate expires on (string): $certExpiresOnString"
[datetime]$expiration = [System.DateTime]::Parse($req.ServicePoint.Certificate.GetExpirationDateString())
#Write-Host "Certificate expires on (datetime): $expiration"
[int]$certExpiresIn = ($expiration - $(get-date)).Days
$certName = $req.ServicePoint.Certificate.GetName()
$certPublicKeyString = $req.ServicePoint.Certificate.GetPublicKeyString()
$certSerialNumber = $req.ServicePoint.Certificate.GetSerialNumberString()
$certThumbprint = $req.ServicePoint.Certificate.GetCertHashString()
$certEffectiveDate = $req.ServicePoint.Certificate.GetEffectiveDateString()
$certIssuer = $req.ServicePoint.Certificate.GetIssuerName()
if ($certExpiresIn -gt $minimumCertAgeDays)
{
if ($ReportType -eq "Html")
{
Write-Host "<td>OKAY</td><td>$certExpiresIn</td><td>$expiration</td><td> </td></tr>"
}
if ($ReportType -eq "Text")
{
Write-Host OKAY: Cert for site $url expires in $certExpiresIn days [on $expiration] -f Green
}
if ($ReportType -eq "PSObject")
{
$returnData += new-object psobject -property #{Url = $url; CheckResult = "OKAY"; CertExpiresInDays = [int]$certExpiresIn; ExpirationOn = [datetime]$expiration; Details = [string]$null}
}
}
else
{
$details = ""
$details += "Cert for site $url expires in $certExpiresIn days [on $expiration]`n"
$details += "Threshold is $minimumCertAgeDays days. Check details:`n"
$details += "Cert name: $certName`n"
$details += "Cert public key: $certPublicKeyString`n"
$details += "Cert serial number: $certSerialNumber`n"
$details += "Cert thumbprint: $certThumbprint`n"
$details += "Cert effective date: $certEffectiveDate`n"
$details += "Cert issuer: $certIssuer"
if ($ReportType -eq "Html")
{
Write-Host "<td>WARNING</td><td>$certExpiresIn</td><td>$expiration</td>"
$stringHtmlEncoded = [System.Web.HttpUtility]::HtmlEncode($details) -replace "`n", "<br />"
Write-Host "<tr><td>$stringHtmlEncoded</td></tr>"
}
if ($ReportType -eq "Text")
{
Write-Host WARNING: $details -f Red
}
if ($ReportType -eq "PSObject")
{
$returnData += new-object psobject -property #{Url = $url; CheckResult = "WARNING"; CertExpiresInDays = [int]$certExpiresIn; ExpirationOn = [datetime]$expiration; Details = $details}
}
rv expiration
rv certExpiresIn
}
}
else
{
if ($ReportType -eq "Html")
{
Write-Host "<td>ERROR</td><td>N/A</td><td>N/A</td>"
$stringHtmlEncoded = [System.Web.HttpUtility]::HtmlEncode($details) -replace "`n", "<br />"
Write-Host "<tr><td>$stringHtmlEncoded</td></tr>"
}
if ($ReportType -eq "Text")
{
Write-Host ERROR: $details -f Red
}
if ($ReportType -eq "PSObject")
{
$returnData += new-object psobject -property #{Url = $url; CheckResult = "ERROR"; CertExpiresInDays = $null; ExpirationOn = $null; Details = $details}
}
}
if ($ReportType -eq "Text") { Write-Host }
rv req
return $returnData
}
#disabling the cert validation check. This is what makes this whole thing work with invalid certs...
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
if ($ReportType -eq "Html")
{
Write-Host "<table><tr><th>URL</th><th>Check result</th><th>Expires in days</th><th>Expires on</th><th>Details</th></tr>"
Add-Type -AssemblyName System.Web
}
}
Process
{
if ($_ -ne $null)
{
CheckUrl $_ $returnData
$ProcessedInputPipeLineByArrayItem = $true
}
}
End
{
if ($ProcessedInputPipeLineByArrayItem -eq $false)
{
if ($Urls -eq $null)
{
$allUrls = get-content $UrlsFile
}
else
{
$allUrls = $Urls
}
foreach ($url in $allUrls)
{
$returnData = CheckUrl $url $returnData
}
}
if ($ReportType -eq "Html") { Write-Host "</table>" }
if ($ReportType -eq "PSObject") { return $returnData }
}
Output might look like e.g.:
"http://www.doma.com", "https://www.domb.com" | .\Check-ExpiringSslCerts.ps1 -ReportType PSObject | ft
Url ExpirationOn CertExpiresInDays CheckResult Details
--- ------------ ----------------- ----------- -------
http://www.doma.com ERROR No certificate in use for connection
https://www.domb.com 18.11.2017 09:33:00 87 OKAY
Put the whole code you've wrote in a script-block, in order to do so, just add at the beginning this code:
$sb = {
and at the bottom of your code add:
}
Once you have this script-block, you can run this script on the servers remotely using these commands:
$cred = Get-Credential
$servers = get-content D:\Certificate.txt
Invoke-Command -Credential $cred -ComputerName $servers -ScriptBlock $SB
Hope it helped!
Your code snippets are helpful but they will throw an error on HTTP errors -- that is the nature of the underlying .NET HttpWebRequest object to panic on everything that's not code 200.
So, if you're testing, say, API endpoints that only accept POST HTTP verb, your script will fail as it will get a 405 error message. Alternatively, if your URL returns HTTP 404, your script will fail as well. Luckily, you can catch layer 7 errors and capture the required info anyway just patch your code in this manner:
try {$req.GetResponse() | Out-Null } catch {
if ($_.Exception.InnerException.Status -eq 'ProtocolError') {
# saving the info anyway since this is a L7 error, e.g.:
$certExpiresOnString = $req.ServicePoint.Certificate.GetExpirationDateString()
[datetime]$expiration [System.DateTime]::Parse($req.ServicePoint.Certificate.GetExpirationDateString())
$certIssuer = $req.ServicePoint.Certificate.GetIssuerName() # ...
} else {
# this is a real error - timeout, DNS failure etc
Write-Host "$url, $_" -ForegroundColor Red
Continue
}
}
Taken mostly from https://gist.github.com/jstangroome/5945820, although with some changes. The following will obtain the certificate. $certificate.NotBefore and $certificate.NotAfter will then need to be checked.
function GetCertificate([string]$domain, [Int16]$port) {
$certificate = $null
$TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
$TcpClient.ReceiveTimeout = 1000
$TcpClient.SendTimeout = 1000
try {
$TcpClient.Connect($domain, $port)
$TcpStream = $TcpClient.GetStream()
$Callback = { param($sender, $cert, $chain, $errors) return $true }
$SslStream = New-Object -TypeName System.Net.Security.SslStream -ArgumentList #($TcpStream, $true, $Callback)
try {
$SslStream.AuthenticateAsClient($domain)
$certificate = $SslStream.RemoteCertificate
}
finally {
$SslStream.Dispose()
}
}
finally {
$TcpClient.Dispose()
}
if ($certificate) {
if ($certificate -isnot [System.Security.Cryptography.X509Certificates.X509Certificate2]) {
$certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certificate
}
}
return $certificate
}

Application runs in Windows 7 but not in Windows 10

I have created an app for back up and restore of computers. I also allows modification of ADObjects through the use of custom Profile.ps1 file. The app runs fine in the ISE with no errors and works properly no errors in Windows 7. However, when I try to run it in a newly imaged Windows 10 machine I get "Property Can Not Be Found" errors on all my object properties.
The thing is I can read all the properties when I fill comboboxes fine. The error only occurs when the form is submitted. I will attach 1 of the forms I am having a problem with. Again it runs fine in Windows 7, but not Windows 10.
Could this be a problem with Microsoft updates?
Also, yes, I am setting Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted.
Error message:
The property 'company' cannot be found on this object. Verify that the
property exist and can be set.
+ $CO.company = $company
+ Categoryinfo :InvalidOperation: (:) [] RuntimeExeption
Code:
. \\iotsdsp01pw\installs$\MoveToOU\PcDeployment\Profile.ps1
#region Validation Functions
function Validate-IsEmail ([string]$Email) {
return $Email -match "^(?("")("".+?""#)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])#))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
}
function Validate-IsURL ([string]$Url) {
if ($Url -eq $null) {
return $false
}
return $Url -match "^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$"
}
function Validate-IsName ([string]$Name, [int]$MaxLength) {
if ($MaxLength -eq $null -or $MaxLength -le 0) {
#Set default length to 40
$MaxLength = 40
}
return $Name -match "^[a-zA-Z''-'\s]{1,$MaxLength}$"
}
function Validate-IsIP ([string]$IP) {
return $IP -match "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
}
function Validate-IsEmptyTrim ([string]$Text) {
if ($text -eq $null -or $text.Trim().Length -eq 0) {
return $true
}
return $false
}
function Validate-IsEmpty ([string]$Text) {
return [string]::IsNullOrEmpty($Text)
}
function Validate-IsDate ([string]$Date) {
return [DateTime]::TryParse($Date, [ref](New-Object System.DateTime))
}
#endregion
$No_Load = {
$NewForm.Close()
#Initialize variables
$dateTime = Get-Date -Format G
$userName = (Get-WmiObject -Class Win32_ComputerSystem -Property UserName).UserName
$computerName = $env:computername
#Varables for display
$distinguishedName = (Get-dn computer cn $computerName)
$computerObject = (Get-ADObject $distinguishedName)
$organizationalUnit = (Get-ADObject "OU=Agencies, DC=state, DC=in, DC=us")
#Initialize Form Controls
$lblUserNameNewNo.Text = $userName
$lblComputerNameNewNo.Text = $computerName
$lblPhysicalLocationNewNo.Text = $computerObject.location
$txtBillingCodeNewNo.Text = $computerObject.departmentNumber
$comboboxAccountTypeNewNo.Text = $computerObject.extensionAttribute15
$comboboxOrganizationalUnitNewNo.Text = $computerObject.company
Load-ComboBox -ComboBox $comboboxOrganizationalUnitNewNo ($organizationalUnit.children | %{ $_.OU })
}
#region Control Helper Functions
function Load-ComboBox {
Param (
[ValidateNotNull()]
[Parameter(Mandatory = $true)]
[System.Windows.Forms.ComboBox]$ComboBox,
[ValidateNotNull()]
[Parameter(Mandatory = $true)]
$Items,
[Parameter(Mandatory = $false)]
[string]$DisplayMember,
[switch]$Append
)
if (-not $Append) {
$ComboBox.Items.Clear()
}
if ($Items -is [Object[]]) {
$ComboBox.Items.AddRange($Items)
} elseif ($Items -is [Array]) {
$ComboBox.BeginUpdate()
foreach ($obj in $Items) {
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
} else {
$ComboBox.Items.Add($Items)
}
$ComboBox.DisplayMember = $DisplayMember
}
#Validation
function ParameterValidate {
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, 10)]
[String]$Text
)
return $true
}
$comboboxOrganizationalUnitNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
#Check if the Name field is empty
$result = Validate-IsEmptyTrim $comboboxOrganizationalUnitNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($comboboxOrganizationalUnitNewNo, "Please select agency.");
} else {
#Clear the error message
$errorprovider1.SetError($comboboxOrganizationalUnitNewNo, "");
}
}
$txtBillingCodeNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
#Check if the Name field is empty
$result = Validate-IsEmptyTrim $txtBillingCodeNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($txtBillingCodeNewNo, "Please enter billing code.");
} else {
#Clear the error message
$errorprovider1.SetError($txtBillingCodeNewNo, "");
}
}
$comboboxAccountTypeNewNo_Validating = [System.ComponentModel.CancelEventHandler]{
$result = Validate-IsEmptyTrim $comboboxAccountTypeNewNo.Text
if ($result -eq $true) {
#Mark a failure only if the Validation failed
$script:ValidationFailed = $true
#Display an error message
$errorprovider1.SetError($comboboxAccountTypeNewNo, "Please enter agency type.");
} else {
#Clear the error message
$errorprovider1.SetError($comboboxAccountTypeNewNo, "");
}
}
$control_Validated = {
#Pass the calling control and clear error message
$errorprovider1.SetError($this, "");
}
$No_FormClosing = [System.Windows.Forms.FormClosingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]
#Validate only on OK Button
if ($No.DialogResult -eq "OK") {
#Init the Validation Failed Variable
$script:ValidationFailed = $false
#Validate the Child Control and Cancel if any fail
$No.ValidateChildren()
#Cancel if Validation Failed
$_.Cancel = $script:ValidationFailed
}
}
#Events
$buttonColor_Click = {
#TODO: Place custom script here
$colordialog1.ShowDialog()
}
$linklblViewListNewNo_LinkClicked = [System.Windows.Forms.LinkLabelLinkClickedEventHandler]{
Start-Process "http://billingcodes.iot/"
}
$btnSubmitNewNo_Click = {
#TODO: Place custom script here
$company = $comboboxOrganizationalUnitNewNo.Text
$departmentNumber = $txtBillingCodeNewNo.Text
$accountType = $comboboxAccountTypeNewNo.Text
if ($accountType -eq "Seat") {
$accountType = " "
}
#Varables for Set-ADObject
$computerObject.company = $company
$computerObject.departmentNumber = $departmentNumber
$computerObject.extensionAttribute15 = $accountType
try {
$computerObject.SetInfo()
[Environment]::Exit(1)
} catch {
$labelDialogRedNewNo.Text = "AD computer object not found"
}
}
This is your culprit (from what I can see):
$No_Load = {
...
$computerObject = (Get-ADObject $distinguishedName)
...
}
...
$btnSubmitNewNo_Click = {
...
$computerObject.company = $company
...
}
You assign a computer object to the variable $computerObject in one scriptblock, and try to change one of its properties in another scriptblock. However, to be able to share a variable between scriptblocks you need to make it a global variable, otherwise you have two separate local variables that know nothing about each other.
$No_Load = {
...
$global:computerObject = Get-ADObject $distinguishedName
...
}
...
$btnSubmitNewNo_Click = {
...
$global:computerObject.company = $company
...
}
BTW, I doubt that this ever worked in Windows 7, since it failed with the same error on my Windows 7 test box.

WMI Warranty information

I have a Powershell script(1) that collects the Warranty information for a Dell system and will display it to the screen. I also have a second script(2) that creates a WMI Namespace and (3)one for a class. What I am trying to do it to get the information gathered by the warranty script to end up in the WMI class so we can pull it through SCCM. The code below is what I have so far. Any help would be greatly appreciated.
(1)
{
$service = New-WebServiceProxy -Uri http://{143.166.84.118}/services/assetservice.asmx?WSDL
if($args.count -ne 0){
$serial = $args[0]
}
else {
$system = Get-WmiObject win32_SystemEnclosure
$serial = $system.serialnumber
}
$guid = [guid]::NewGuid()
$info = $service.GetAssetInformation($guid,'check_warranty.ps1',$serial)
if($info.count -eq 0)
{
Write-Host "Machine is not a Dell"
}
else
{
$warranty = $info[0].Entitlements[0]
$expires = $warranty.EndDate
$days_left = $warranty.DaysLeft
if($days_left -eq 0)
{
Write-Host "Warranty has expired"
}
else{
`enter code here`
set objwmiservice = getobject("winmgts:\\computer\root\warranty")
set colitems = objwmiservice.execquery("select * from warranty_warranty")
objitem.Warranty = $expires
objitem.put_
next
Write-Host "Warranty expires $expires."
Write-Host "Warranty is valid for $days_left days."
}
}
}
(2)
'Create namespace
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root")
Set objItem = objWMIService.Get("__Namespace")
Set objNamespace = objItem.SpawnInstance_
objNamespace.Name = "Warranty"
objNamespace.Put_ '
(3)
' $newClass = New-Object System.Management.ManagementClass `
("root\Warranty", [String]::Empty, $null);
$newClass["__CLASS"] = "Warranty";
$newClass.Qualifiers.Add("Static", $true)
$newClass.Properties.Add("Warranty", `
[System.Management.CimType]::String, $false)
$newClass.Properties["Warranty"].Qualifiers.Add("Key", $true)
$newClass.Put()'`enter code here`