Wednesday, December 1, 2021

Using Powershell for the FordPass API

You can use the FordPass for many models of Ford. Is it possible to use Powershell to get the battery status of a eletric car or PHEV? I found some solutions:  

clarkd/fordpass-python: Python wrapper for the FordPass API for Ford vehicle information and control: start, stop, lock, unlock. (github.com)

itchannel/fordpass-ha: Fordpass integration for Home Assistant (github.com)

Both solutions are made in Python. I like to use Powershell. So I made a early version of a script to get the battery status with Powershell:

#VIN:
$Vin = "<VIN>"
#Status URL:
$URL = "https://usapi.cv.ford.com/api/vehicles/v4/$Vin/status"
# Headers for token request
$Data = @{
"client_id" = "9fb503e0-715b-47e8-adfd-ad4b7770f73b"
"grant_type"= "password"
"username" = "<USERNAME>"
"password" = "<PASSWORD>"
}
$TokenHeader = @{
"Accept" = "*/*"
"Accept-Language" = "en-us"
"User-Agent" = "fordpass-na/353 CFNetwork/1121.2.2 Darwin/19.3.0"
"Accept-Encoding" = "gzip, deflate, br"
"Content-Type" = "application/x-www-form-urlencoded"
}
# Request for token:
$Result = Invoke-webrequest -Uri 'https://fcis.ice.ibmcloud.com/v1.0/endpoint/default/token' -Headers $TokenHeader -Body $Data -Method Post
$Token = ($Result.Content | convertfrom-json).access_token
#Header with token:
$StatusHeader = @{
"Application-Id" = "71A3AD0A-CF46-4CCF-B473-FC7FE5BC4592"
"Content-Type" = "application/json"
"auth-token" = $Token
}
# Get battery state:
$VehicleInfo = Invoke-RestMethod $URL -Headers $StatusHeader
$VehicleInfo.vehiclestatus.batteryFillLevel
view raw gistfile1.txt hosted with ❤ by GitHub
This is the end result:


In the coming weeks I will test more options from the API and post them here.

Thursday, October 21, 2021

WSUS Part 3: Tuning you WSUS server with Powershell

WSUS Server cleanup

Because the WSUS server uses a lot of storage, it can be useful to clean the WSUS server regularly. This works well if you starts with cleaning regularly after a clean install. When you wait to long and the WSUS server becomes bigger and bigger, it will take a long time to clean the server. In PowerShell you can use this cmdlet:

Invoke-WsusServerCleanUp 

Parameters:

-CleanupObsoleteComputers

Specifies that the cmdlet deletes obsolete computers from the database.

-CleanupObsoleteUpdates

Specifies that the cmdlet deletes obsolete updates from the database.

-CleanupUnneededContentFiles

Specifies that the cmdlet deletes unneeded update files.

-CompressUpdates

Specifies that the cmdlet deletes obsolete revisions to updates from the database.

-DeclineExpiredUpdates

Specifies that the cmdlet declines expired updates.

-DeclineSupersededUpdates

Specifies that the cmdlet declines superseded updates.

-UpdateServer

Specifies the object that contains the WSUS server. This value is obtained by calling the Get-WsusServer cmdlet and passing the resulting IUpdateServer object into this cmdlet.

Example I use for my script:

Invoke-WsusServerCleanUp -CleanUpObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates


Wednesday, October 13, 2021

WSUS Part 2: Create a task for Patch Tuesday with Powershell

I found out that it is not possible to choose a monthly schedule on the second tuesday of the month with "New-ScheduledTaskTrigger". It is only possible to use a monthly or weekly schedule. Because of this, I used SCHTASKS to create a task for my WSUS PowerShell scripts:

SCHTASKS /RU "NT AUTHORITY\SYSTEM" /Create /ST 18:00 /SC Monthly /MO Second /D Tue /TN "<TASKNAME>" /TR "PowerShell.exe C:\Scripts\<ScriptName>.ps1 -ExecutionPolicy Bypass"

For more information about "New-ScheduledTaskTrigger":


Wednesday, October 6, 2021

WSUS Part 1: Synchronization updates and Approve/Deny updates with Powershell

You can use PowerShell for WSUS management. This example works on a Windows Server with Powershell 5.1 and the Powershell WSUS module installed from Server Manager. You can start the synchronization and approve the updates with the example below:

$wsus = Get-WSUSserver
$Subscription = $wsus.GetSubscription()
# Start de WSUS Synchronization.
$Subscription.StartSynchronization()
# Wait Until the the Synchronizations is finished.
Start-Sleep -Seconds 5
While($Subscription.GetSynchronizationProgress().Phase -ne "NotProcessing"){
Write-Host "Synchronization is busy: $($Subscription.GetSynchronizationProgress())"
Start-Sleep -Seconds 5
}
Get-WSUSUpdate -Approval AnyExceptDeclined | Approve-WSUSUpdate -Action Install -TargetGroupName "All Computers"
view raw WSUS-Sync.ps1 hosted with ❤ by GitHub

You can use this example if you like to deny older updates:

# Get all updates older then 4 months:
$Updates = Get-WSUSUpdates -Approval AnyExceptDeclined | Where-Object{!($_.update.creationdate -ge (Get-Date).Addmonths(-4))}
# Deny the updates:
$Updates | Deny-WSUSupdate
view raw WSUS-Deny.ps1 hosted with ❤ by GitHub
All updates are denied when they are older then 4 months.

Sometimes it is necessary to accept a license agreement for an update. You can use this code to accept all license agreements:

$wsus = Get-WSUSServer
# Get a list of updates with a License Agreement.
$AcceptLicenses = $wsus.GetUpdates() | Where-Object {$_.HasLicenseAgreement -eq "True"}
# Accept the License Agreement for the updates.
ForEach($Item in $AcceptLicenses){
$item.AcceptLicenseAgreement()
}
view raw WSUS-License hosted with ❤ by GitHub

Wednesday, September 29, 2021

Password generators for Powershell

There are several ways to create a random password in your PowerShell script. 

Powershell function: 

A function to create an random password string example:

function Get-RandomPassword($length, $characters) {
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
$private:ofs=""
return [String]$characters[$random]
}
Get-RandomPassword -length 15 -characters "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#"

Powershell script:

You can use a this script for more advanced functions:

GitHub - PowerShell random password generator

API:

You can use an API:

GitHub - fawazsullia/password-generator

Example:

$url = 'https://passwordinator.herokuapp.com/generate?num=true&char=true&caps=true&len=14'
$password = (Invoke-WebRequest -Uri $url ).content | ConvertFrom-Json
$password.data



Thursday, September 23, 2021

Restore a file with Powershell

Did you delete the wrong file? Need to restore an accidentally deleted file? This Powershell Module
can be the solution:

PowerForensics


PowerForensics is a free Powershell module for forensic research with Powershell. You can use this module for restoring deleted files. Of course there is no guarantee that you can restore it, but you can try it. Check this nice tutorial:

How to Recover Deleted Files from the Drive? | CQURE Academy



Thursday, September 2, 2021

How to create an ISO with Powershell

I don't like to install ISO creation software on every jump or management server. I use a script from this website:


The code is easy to copy to a server in a RDP session or copy a PS1 file to a share and you can run on the server. You don't need any additional modules.


Sunday, August 29, 2021

PowerShell: Download updates from the Microsoft Update Catalog

Download updates from the Microsoft Update Catalog

Most administrators use WSUS for deploying updates on a network. But sometimes it is useful to download stand-alone updates from the Microsoft Update Catalog. I found this PowerShell module:

 https://github.com/ryan-jan/MSCatalog

It is regularly updated and works fine for me. Soon I will post some examples.