Thursday, July 25, 2024

Test-NetConnection using custom source port.

Powershell utility to test remote port using a custom source address from your computer.

 

If you have multiple ip addresses assigned on your computer the Test-Netconnection utility does not allow you to test the connection to a remote computer using  one of those addresses. 


The script below addresses that drawback.

 #Usage: .\Test-Connect.ps1 -computername 10.10.10.10 -RemotePort 389 -sourceipAddress 123.23.10.5

param([string]$computername,[string]$RemotePort,[string]$sourceipAddress)
$destHostName = $computername
$destPort     = $RemotePort
$src  = [System.Net.IPEndPoint]::new([ipaddress]::Parse($sourceipAddress),0)
$tc   = [System.Net.Sockets.TcpClient]::new($src)
$tc.Connect($destHostName,$destPort)

if ($tc.Connected) {
    "Connected!"
} else {
    "Not connected"
}

$tc.Dispose()

Thursday, April 25, 2024

Connect Scom powershell using SDK

 The SDK binaries are stored in the following folder on the SCOM MS

C:\Program Files\Microsoft System Center\Operations Manager\Server\SDK Binaries

copy the three dll files to your script folder.

Sample script.

##########################################################################

$rms= "RMS.contoso.com"
$UserName = "domain\username"
$pwd = "password"

$scriptdir = "C:\Scripts"
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.OperationsManager.dll")
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.Core.dll")
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.Runtime.dll")
 $securePassword = ConvertTo-SecureString $pwd –AsPlainText -Force
$MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings($rms)
$MGConnSetting.UserName = $UserName
$MGConnSetting.Domain = $UserDomain
$MGConnSetting.Password = $SecurePassword
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)
if(!$MG) {Write-Host "Cannot work on $rms" -ForegroundColor Yellow;continue;}
#######################
Write-host "The Management group is " -nonewline; Write-Host "$MG" -ForegroundColor Yellow

$MG.Administration.GetAllAgentManagedComputers()

###################################################################################

 If you don't use Microsoft.EnterpriseManagement.Runtime.dll binary you will get this error.

New-Object : Exception calling ".ctor" with "1" argument(s): "The service type 'Microsoft.EnterpriseManagement.Runtime.ITaskRuntimeService, Microsoft.EnterpriseManagement.Runtime, Culture="", PublicKeyToken=31bf3856ad364e35,
Version=7.0.5000.0' for the component named 'TaskService' cannot be found."
At line:1 char:1
+ New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSett ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

 

Friday, March 22, 2024

Powershell Export-Csv for PSCustomobject which is a list of strings as property.

 example powershell customobject

$myObject = [PSCustomObject]@{
    Name     = 'Parag'
    Language = 'PowerShell'
    State    = 'Washington'
}

To export this object to csv we can use.

$myobject | Export-csv -path c:\temp\temp.csv -NoClobber -NoTypeInformation

The file will show up like this

 Name,Language,State

Parag,Powershell,Washington

But if the properties of the object are a list,i.e I know more than one languages then the file shows up as 

Name,Language,State

Parag,System.Collections.Generic.List`1[System.String],Washington

 That is because the command treats the property as an array. You can see the same behavior when you export an array.

To export this data properly we can do this.

$myobject | Select-Object Name,@{n='Language';e={$_.Language -join ' ' }},State

Now in the file we will see

 

 Name,Language,State

Parag,Powershell JavaScript,Washington