# Create PSCredential object for authentication.
#
$SECURE_PASSWORD = ConvertTo-SecureString "$TARGET_PASSWORD" `
                                          -AsPlainText -Force;
$CREDENTIAL_OBJECT = `
        New-Object System.Management.Automation.PSCredential `
                   $TARGET_USER, $SECURE_PASSWORD;
 
# Start a new WMI session and store the connection information
# as $session. The protocol used can be either DCOM (RPC using
# TCP 135, as with sc.exe and schtasks.exe) or WSMAN (WinRM
# over TCP 5985 or TCP 5986, depending on whether HTTPS is
# supported by $TARGET_HOST).
#
$OPTIONS_OBJECT = New-CimSessionOption -Protocol DCOM
$SESSION_OBJECT = New-Cimsession `
                      -ComputerName $TARGET_HOST `
                      -Credential $CREDENTIAL_OBJECT `
                      -SessionOption $OPTIONS_OBJECT `
                      -ErrorAction Stop