Unix Time conversion with powershell - powershell

Import-Module SQLite
mount-sqlite -name places -data (resolve-path C:\Users\malware_win7x86\Desktop\places.sqlite)
$places = Get-ChildItem places:\moz_places
foreach ($entry in $places)
{
[datetime]$origin = '1970-01-01 00:00:00'
$visited = $entry.last_visit_date
if ($visited) {
$vtime = $origin.AddSeconds($visited)
} else {
$vtime = 'testing'
}
write-host ""
$entry.url,
$entry.visit_count,
$vtime
}
Reference: Convert Unix time with PowerShell
I'm curious on how to handle entries with no data.
For example, here is some output from that script:
http://sysforensics.org/
1
Exception calling "AddSeconds" with "1" argument(s): "Value to add was out of range.
Parameter name: value"
At line:7 char:28
+ $vtime = $origin.AddSeconds <<<< ($visited)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Any ideas on this?
Thanks

That error looks like you're giving AddSeconds an argument that is either too big or too small, rather than missing data. Calling AddSeconds with either $null or 0 will return the original date unchanged.
That being said—and I can't be sure, since you haven't provided the actual integer that you're trying to convert—I had the same problem until I realized that the data I was receiving was in milliseconds, not seconds. You can either use AddMilliseconds instead, or divide the number by 1000 before using AddSeconds if you don't care about the loss of precision.
Credit to this answer for pointing me in the right direction.

In that case you'd just skip the method call and substitute a default value, e.g.:
if ($visited) { # or maybe $ivisited -is [int] or whatever your criterion is
$vtime = $origin.AddSeconds($visited)
} else {
$vtime = $null
}

Related

Convert a string variable into an integer

I'm trying to convert my $file_data variable into an integer, the bit of code below grabs the number of a computer on my system held in a directory. However when run in PowerShell I get he below error even though the variable is a number.
The '++' operator works only on numbers. The operand is a 'System.String'.
At D:\pscript\Intune.ps1:7 char:26
+ For ($file_data -le 130; $file_data++ ){
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : OperatorRequiresNumber
I'm not sure where I'm going wrong on this any help would be amazing. :)
Get-Content D:\pscript\temp\Directory.txt
$file_data = Get-Content D:\pscript\temp\Directory.txt
For converting string to integer, you can typecast it or declare it at the first point.
[int] $file_data = Get-Content D:\pscript\temp\Directory.txt
However, if this needs to work, Directory.txt should have number which can fit into the category of an integer.

Add elements to arry in powershell

I want to simply add some numbers to an array and then sort them via powershell, however, the following code seems to be wrong
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
$myArray.Add($Tokens[$Tokens.Count-1])
}
Write-Host($myArray | Sort-Object)
The error is
+ $myArray.Add($Tokens[$Tokens.Count-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
How can I fix that?
The variable $Name is something like 101.u18.uab.14 or 103.win10.template or 102.win7.pink.18 and so on. Each $Name has some . symbols and I want to tokenize them and get the last element for each of them. So, in this example, I want to see a sorted 14 18 template.
UPDATE:
The provided methods seems to be incorrect.
1- This method by Steven
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
shows this error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:20 char:5
+ [Void]$myArray.Add($Tokens[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
2- This method by Santiago
$myArray = [Collections.Generic.List[string]]::new()
Foreach ($Name in $VMName) {
[Void]$myArray.Add($Name.Split(".")[-1])
}
Shows the following error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:19 char:5
+ [Void]$myArray.Add($Name.Split(".")[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
If I have missed your point in the above codes, please let me know.
I think you are missing the first line from the error. However it looks like you are simply trying to add the last elements from the $Tokens array. In that case you don't need to reference the index like that, below should work:
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
Notice the addition of [Void] this will stop the .Add() method from returning the index number it just added to.
Also note you can create array list objects using casting like:
$myArray = [Collections.ArrayList]#()
Update to Address Continued Errors:
The only thing I can think of to cause the error "Collection was of a fixed size." is if you've previously type constrained the variable.
Example:
[String[]]$myArray = #()
# Posibly a whole bunch of other things happening maybe in the console or IDE.
$myArray = [Collections.ArrayList]#()
$myArray.Add('something')
Results:
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $myArray.Add('something')...
In this case they type of the $myArray will not change to [Collections.ArrayList]. The problem will be transparent up until you try to use the .Add() method that won't work. This is because an array list is easily and therefore silently cast back to a [String[]] or [Object[]].
Note: If you were to run $myArray.IsFixedSize it would return "True".
My guess as to what's happening; at some point while developing your code or perhaps in the larger script, $myArray got type constrained, and stuck in the scope. This can definitely happen especially given the scope overlap in IDE's like PowerShell's ISE, and I think it happens in VSCode as well. If this is part of a larger script look for instances of $myArray to see if it's indeed type constrained and make corrections as needed. Otherwise a simply restarting your session might do the trick.
Honestly, not sure how could you be getting that error unless the array we're looping through is actually something different. Steven's answer should work fine, I'll put this code below just to show that the results we get are the ones you expect:
$col = [Collections.Generic.List[String]]::new()
$vmName = #(
'101.u18.uab.14'
'103.win10.template'
'102.win7.pink.18'
)
ForEach($name in $vmName)
{
$col.Add($name.Split('.')[-1])
}
if you want absolutly use array you can simply do it :
$Array=#()
$VMName | %{
$Value=($_.Split('.'))[-1]
$Array+= $Value
}
$Array| sort
Otherwise you can simply do it :
$VMName | %{($_.Split('.'))[-1]} | sort

Powershell Catch print modified error only

I have the csv file with the following values:
User,TimeStamp
Pinky,11/4/2015 5:00
Brain,
Leo,never
Don,unspecified
I want to ensure this file for the TimeStamp column either has a date, or a $null value. To do this I am using the following code:
Function HealthCheckTimeStampColumn
{
Param ($userInputCsvFileWithPath)
Write-Host "Checking if TimeStamp column has invalid values..."
Import-Csv $userInputCsvFileWithPath | %{
if ($_.TimeStamp)
{
Try
{
([datetime]$_.TimeStamp).Ticks | Out-Null
}
Catch [system.exception]
{
$Error.Clear()
$invalidValue = $_.TimeStamp
Write-Error "Invalid Value found `"$_.TimeStamp`"; Value expected Date or `"`""
Exit
}
}
}
Write-Host "All values were found valid."
Write-Host "TimeStamp Healthcheck Column Passed"
Write-Host ""
}
With this code, I get this error:
Invalid Value found "Cannot convert value "never" to type "System.DateTime". Error: "The string was not recognized as
a valid DateTime. There is an unknown word starting at index 0.".TimeStamp"; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
If I try this line of code Instead:
Write-Error "Invalid Value found `"$invalidValue`"; Value expected Date or `"`""
I get this error:
Invalid Value found ""; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
The error I am expecting to see is this:
Invalid Value found "never"; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Can anyone tell me what I am doing wrong?
You don't really need a try/catch block for this one either. They are good for unexpected and unavoidable errors. However looking about_Type_Operators you will see -as and -is which handle this situation rather gracefully.
-is : Returns TRUE when the input is an instance of the specified .NET Framework type.
-as : Converts the input to the specified .NET Framework type.
When -as encounters a string or something that is not castable to [datetime] it will return a null. More importantly it will not error. I propose that you check all values for non nulls and invalid date times. Capture all of those in a variable. Then check if the variables has any values. Print all at once! and then exit if you want. I also second what user2460798's answer said about the use of exit.
Function HealthCheckTimeStampColumn{
Param ($userInputCsvFileWithPath)
$badRows = Import-Csv $userInputCsvFileWithPath |
Where-Object{-not [string]::IsNullOrEmpty($_.TimeStamp) -and ($_.TimeStamp -as [datetime]) -eq $null}
if($badRows){
$badRows | ForEach-Object{
Write-Host "'$($_.Timestamp)' is not a valid datetime" -ForegroundColor Red
}
Write-Error "$($badRows.Count) Invalid Value(s) found"
} else {
"All values were found valid.","TimeStamp Healthcheck Column Passed","" | Write-Host
}
}
Turning PerSerAl's observation into an answer:
$_ changes meaning from when it is in the foreach-object loop (but outside catch block) to when it is in catchblock. In the first case it is the current object (row), which apparently has the value "never" for its timestamp. But in the catch block it is the errorrecord that was generated as a result of the error. So to fix:
Function HealthCheckTimeStampColumn
{
Param ($userInputCsvFileWithPath)
Write-Host "Checking if TimeStamp column has invalid values..."
Import-Csv $userInputCsvFileWithPath | %{
$row = $_
if ($_.TimeStamp)
{
Try
{
([datetime]$_.TimeStamp).Ticks | Out-Null
}
Catch [system.exception]
{
$Error.Clear()
$invalidValue = $_.TimeStamp
Write-Error "Invalid Value found `"$row.TimeStamp`"; Value expected Date or `"`""
Exit
}
}
}
Write-Host "All values were found valid."
Write-Host "TimeStamp Healthcheck Column Passed"
Write-Host ""
}
BTW, if you want to process the whole file you'll need to remove exit from the catch block.

Weird PowerShell Exec Output Capture Behavior

I'm writing a simple PowerShell script that handles the output of mkvinfo. It captures the output of mkvinfo, stores in a variable $s and does some post-processing on $s. The strange part is while $s has content, I can't extract a substring from it.
The error message I'm getting was:
Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
This is a sample code:
$filePath = $folder + $file.name
$mkvinfoExe = "C:\mkvinfo.exe"
$s = & $mkvinfoExe $filePath
$s | out-host
$s.Substring($s.Length-1) | out-host
Are you sure $s is a string and not an array? If it is an array, $s.Length will be the number of elements in the array and you could get the error that you are getting.
For example:
PS > $str = #("this", "is", "a")
PS > $str.SubString($str.Length - 1)
Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger than length of string.
Parameter name: startIndex"
At line:1 char:1
+ $str.SubString($str.Length - 1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
Just found out because mkvinfo outputs multiple lines, $s is actually a String array (or List?). Switching to $s[0].Substring($s[0].Length-1) solves it.

First parameter in PowerShell function loses its value?

Here's a function that I call with .\GetEMSInstallers. For some unknown reason the first parameter always loses its value:
function Get-EMSInstallers {
param (
$ems_for_amx_source = '\\server\ems_path',
$installers_dir = 'D:\installers'
)
process {
if (!(Test-Path "$installers_dir\EMS4AMX")) {
"Copying files and folders from $ems_for_amx_source to $installers_dir\EMS4AMX"
copy $ems_for_amx_source "$installers_dir\EMS4AMX" -rec -force
}
}
}
Get-EMSInstallers $args
When I call it I get this output:
Copying files and folders from to D:\installers\EMS4AMX
Copy-Item : Cannot bind argument to parameter 'Path' because it is an empty array.
At C:\Users\ad_ctjares\Desktop\Scripts\Ems\GetEMSInstallers.ps1:12 char:17
+ copy <<<< $ems_for_amx_source "$installers_dir\EMS4AMX" -rec -force
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,Microsoft.PowerShell.Commands.CopyI
temCommand
When you don't pass in any argument to Get-EMSInstallers you still have a $args array - it is just empty. So the $ems_for_amx_source parameters is set to this empty array.
In other words, one way around this:
if ($args)
{
Get-EMSInstallers $args
}
else
{
Get-EMSInstallers
}
There is probably a more powershelly way to do this - I might revise this later if it comes to mind. :-) But that will get you started anyway.