17 November, 2019

Powershell sending to Google Voice, and also a Lease mileage tool...

Probably, the most useful part of this, is what it takes to send an email to a (your?, some other?) Google Voice (GV) conversation... I suppose.


So that part first...
In GV settings, make sure you have enabled "Forward messages to email".

Then from GV... Send a text message to your GV number.
Head you your GMAIL account, and find that email notification from GV.
Click on the three vertical dots next to the 'reply' arrow, in this email, and select 'Show original':

Scroll down to find the 'From:' line...
This is the email address of this conversation, that you will use in your script:


The rest of this, is the script I have scheduled to run every day, telling me what my target mileage should be less than, and how many days I have left on this 39 month lease.
$ODOatStart = 18
$LeaseMileage = 325000 
$LeaseStarted = "11/16/2019"
$LeaseEnding = "02/16/2023"
$DaysHad = (((Get-date).AddDays(1))-[DateTime]'11/16/2019').Days
$TotalDays = 1189 # = ([DateTime]$LeaseEnding-[DateTime]$LeaseStarted).Days+1
$MilesPerDay = 27.33389 # = ($LeaseMileage/$TotalDays)
$MilesAllowed_Today = '{0:N0}' -f ([math]::Round(($DaysHad*$MilesPerDay)+$ODOatStart))

$Username = "joe.smith" # Just the username. Leave out the '@gmail.com'
$PW = "sr465bblo0986vfert65z" # Application Specific PW (because of 2-factor auth)
$EmailFrom = "joe.smith@gmail.com"
#$EmailTo = 5554441313@tmomail.net
# look at the 'From:' line of the raw email
  $EmailTo = "15551212.15551212.g45ty76vtrq@txt.voice.google.com"
$Subject = "Mileage Target"
$DaysLeft = '{0:N0}' -f ($TotalDays - $DaysHad)
$Body = "ODO: $MilesAllowed_Today  ($DaysLeft days left)"

$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $PW);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

#([datetime]"02/16/2023").ToString(“dddd, dd MMM yyyy”)

Some of the calculations ($TotalDays and $MilesPerDay) are commented out...
Plug in the variables, and (F8) run  just those...
Then run those calculations to get their fixed values.

([DateTime]$LeaseEnding-[DateTime]$LeaseStarted).Days+1
($LeaseMileage/$TotalDays)

Or run the calculations each time... Your call... I don't know your life. :-)

$TotalDays = ([DateTime]$LeaseEnding-[DateTime]$LeaseStarted).Days+1
$MilesPerDay = ($LeaseMileage/$TotalDays)

The only reason I hard code them in, is that these are 'fixed; values, and it makes the script run just a (nominal, but still) bit faster.

You can use this to send texts to a mobile phone directly too... 

The T-Mobile example is commented out.
And you can send to multiple recipients by separating addresses with a single comma, and encapsulating that in double quotes:

$OtherEmail = "5554441313@tmomail.net"
$EmailTo = "$OtherEmail,15551212.15551212.g45ty76vtrq@txt.voice.google.com" 

~ That last line was just me looking at what day of the week the lease ends on.

12 September, 2019

Powershell - Get computers IMEI, and SIM card number ~ Remote PC

I just went through a marathon session, setting up several Dell 7212 Rugged tablets, that came with their SIM card already inserted...

In order to activate the cellular capabilities with a carrier, the following two details are required...
     But these are kind of a hassle to get to. And to work with...
  •      The IMEI number (device ID)
  •      The SIM card number

I found it MUCH easier to get to this info from my own computer, than from the tablet, as follows:
   ~ Note - About the Test-WSMan, PsExec, and the winrm config...
    ~ These just make sure WinRM is enabled. 

     ~ 'Invoke-Command' requires WinRM


$Target = "HOSTNAME"

$TestCommand = $null
$TestCommand = Test-WSMan -ComputerName $Target
If (!($TestCommand)){C:\SysInternals\PsExec.exe -s -nobanner \\$Target /accepteula cmd /c "c:\windows\system32\winrm.cmd quickconfig -quiet"}

$Network_IMEI = Invoke-Command -ComputerName $Target -ScriptBlock { netsh mbn show interfaces }
$Network_SIM = Invoke-Command -ComputerName $Target -ScriptBlock { netsh mbn show read interface=* }
$IMEI = ((($Network_IMEI | select-string "device id").ToString()).Trim()).replace('              ','')
$SIM = ((($Network_SIM | select-string "SIM ICC Id").ToString()).Trim()).replace('       ','')
Write-Host "Cellular device info:" -ForegroundColor Cyan -BackgroundColor DarkGreen
$IMEI
$SIM

There's plenty of extra lines in there, to make it look pretty, but you should get the point, pretty easily.
The two pieces (from what I could see), are in different parts of the mnb. So... Two commands.

It took me a while to figure this all out... To get BOTH the IMEI number, and the SIM card number.

Hope it helps someone else!


27 March, 2019

Active Directory SID lookup.

Some notes on how to figure out what a specific AD SID value belongs to.

I was asked why a specific SID was showing up in an applications Security Alert logs.

That logging system tells the user that it is probably a 'Brute Force' attack from:
CORP\S-1-5-21-436374069-117609710-839522115-6608

I used GetADUser to filter for that SID... Nothing.

I opened up my search, and looked at all AD objects, but the SID value was not apparent.
Here is where I ended up:

$SID_Value = "S-1-5-21-436374069-117609710-839522115-6608"
Get-ADObject -Filter "objectSid -eq '$SID_Value'"

Saw that it WAS a Computer object - So, more details as follows:

Get-ADComputer ((Get-ADObject -Filter "objectSid -eq '$SID_Value'").Name)

04 January, 2019

Powershell array to CSV file

The easiest way to drop a Powershell array into a CSV file...

$Array | Out-File $SaveLocation -Append -Encoding Ascii

Assigning the text format is the trick - the CSV has to be in ANSI.

And just Out-File save it as a CSV