Use powershell to ping and output to log file

Copy following in a batch file, and you could ping multiple address at once and logged it to files:

start powershell.exe -Command "ping -t 8.8.8.8 | ForEach {'{0} - {1}' -f (Get-Date),$_} >> C:\Logs\Dot8.log"
start powershell.exe -Command "ping -t 10.0.0.1 | ForEach {'{0} - {1}' -f (Get-Date),$_} >> C:\Logs\GW.log"
start powershell.exe -Command "ping -t 10.0.0.60 | ForEach {'{0} - {1}' -f (Get-Date),$_} >> C:\Logs\DC.log"

 

Launch another process with Encrypted Credential (Powershell)

First create an AES key and output it to a file. (reference here)

$KeyFile = "C:\ps\s\AESKey\AES.key"
$Key = New-Object Byte[] 16   # You can use 16, 24, or 32 for AES
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile

 
Second, use the AES key we created in the first step to encrypt the password and output it to a file.

$PasswordFile = "C:\ps\s\AESKey\pwd.txt"
$KeyFile = "C:\ps\s\AESKey\AES.key"
$Key = Get-Content $KeyFile
$Password = "Y0urS3curePa$$w0rd." | ConvertTo-SecureString -AsPlainText -Force
$Password | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile

 
Now, you got a password file which can only be decrypted by that Key you created in first step.

Here is a snippet on how to create a powershell encrypted credential, and use it to launch another process.

# use the passwordfile, and key created earlier
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)

# Lauch another process with other credential from the user's powershell session
# This is useful if the user only has normal right, and you could launch it with another credential whom has admin right.
Start-Process powershell.exe -Credential $MyCredential -WindowStyle Hidden -ArgumentList "Start-Process $($Program) -Wait -ErrorAction SilentlyContinue"

 

Please take step to protect that keyfile!

Powershell - Using XML as config file

It is extremely easy to use xml as a config file:

To load values from XML,
For example:

Settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<DefaultValues>
 <Version>22.09.2017.2</Version>
</DefaultValues>


[xml]$Settings = Get-Content "pathto\Settings.xml"
$version = $Settings.DefaultValues.Version

 

Works on Excel files with Powershell

Here is a powershell script where it could "compare and update".
This particular powershell will look for the username and insert it from one file to the other.

* Adjust and use it in your own risk.


$strPath = "C:\s\library.csv"  # this is the main spreadsheet which we look for the username and update the password
$strPath2 = "C:\s\Year102016.csv" # this is the csv file which contains the username and password

$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false

$workbook1 = $objExcel.Workbooks.Open($strPath)
$worksheet1 = $workbook1.sheets.item("library")
$intRowMax1 =  ($worksheet1.UsedRange.Rows).count

$workbook2 = $objExcel.Workbooks.Open($strPath2)
$worksheet2 = $workbook2.sheets.item("Year102016")
$intRowMax2 =  ($worksheet2.UsedRange.Rows).count


$colA = 1 # column contain the username
$colO = 15 # Where we need to insert the password

# 
#
Function checkAndAdd
{

        Param($username, $password)

        write-host "Second FOR Loop: " $intRow1 " Looking for username: " $username

        # Loop from Row 1 to the last Row
        for ($intRow1 = 1 ; $intRow1 -le $intRowMax1 ; $intRow1++) 
        {
            # read the username into $colA
            $colA = $worksheet1.cells.item($intRow1,1).value2 

            if ($colA -match $username)
            {
                Write-Host "Found username: " $username " insert password to column now."
                $worksheet1.cells.item($intRow1, $colO) = $password
                break
            } 
                
         } # ROW FOR LOOP
}



# READ the Year102016.csv row by row, pass in the Username, Password, to the other Excel file
# MAIN For Loop
#

Function ReadUsernamePassword
{ 
    # Loop from Row 2 to the last Row
    for ($intRow2 = 2; $intRow2 -le $intRowMax2 ; $intRow2++) 
    {
        # Make sure column 1 = username, column 8 = password
        $username = $worksheet2.cells.item($intRow2, 1).value2
        $password = $worksheet2.cells.item($intRow2, 8).value2

        
        write-host "MAIN For Loop: "$intRow2 " checking username: " $username " password: " $password 
        checkAndAdd $username $password

    } 
}    
#>


# Check 1 code only
# Beware the strPath file that you read/write to may need to change.

Function Check1Code
{
    Param($username, $password)
    checkAndAdd $username $password
}

## Uncomment this to Run Check1Code  
## 
## 
#
#Check1Code "abc1234" "password"

ReadUsernamePassword

      
$workbook1.SaveAs('C:\s\LibraryFINAL.csv') # output file 
$objexcel.quit()

Secure Password & Key with Powershell

To encrypt credential which works on different computer(s), you need to generate the key and the encrypted password file.

After you have both key and encrypted file, you could run powershell using that credential generated.

Here is the reference which I referred on.

Creating AES key with random data and export to file:

$KeyFile = "\\Machine1\SharedPath\AES.key"
$Key = New-Object Byte[] 16   # You can use 16, 24, or 32 for AES
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile

Creating SecureString object

$PasswordFile = "\\Machine1\SharedPath\Password.txt"
$KeyFile = "\\Machine1\SharedPath\AES.key"
$Key = Get-Content $KeyFile
$Password = "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force
$Password | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile

Creating PSCredential object

$User = "MyUserName"
$PasswordFile = "\\Machine1\SharedPath\Password.txt"
$KeyFile = "\\Machine1\SharedPath\AES.key"
$key = Get-Content $KeyFile
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
 -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)

Example of how to use PSCredential to map network drive:

New-PSDrive -Name P -PSProvider FileSystem -Root $UNC_PATH -Credential $MyCredential -Persist

* Keep the key file somewhere safe, because it is the key that could encrypt/decrypt the password file.

Password encrypted as SecureString in powershell

Create a password file with securestring:

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath C:\encrypted.securestring

Use the password file to create a PSCredential

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "username",(Get-Content -Path C:\encrypted.securestring | ConvertTo-SecureString)

Reference here.

I realized that this encrypted password only work on the machine which encrypting it.
To have this works on a different computer(s), you would need the key and the encrypted file.