Argument Exception with GetVersionInfo and powershell - powershell

I am trying to use powershell to get the file version of a file. If I right click the file and look at the version, it shows a value. Here is how I am trying to do it:
$path = "MSDE2000A";
$info = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);
Here is the exception information it is throwing:
Exception calling "GetVersionInfo" with "1" argument(s): "MSDE2000A.exe"
At line:1 char:58
+ $f = [system.diagnostics.fileversioninfo]::getversioninfo <<<< ("MSDE2000A.exe")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Every file I've checked has the sames result. However, if my path is c:\windows\notepad.exe (as in the example) it works as expected. What's going on?

.NET and PowerShell's notion of current directory aren't always the same. Try passing in the absolute path.
[Diagnostics.FileVersionInfo]::GetVersionInfo('C:\Windows\System32\user32.dll')
ProductVersion FileVersion FileName
-------------- ----------- --------
6.1.7600.16385 6.1.7600.1638... C:\Windows\System32\user32.dll
Also, you can get this information with Get-ChildItem like so:
Get-ChildItem C:\Windows\System32\user32.dll | fl VersionInfo

Related

powershell get-childitem path format is not supported error

Powershell 5.1 on Windows 7 sp1 throws the error below:
Snippet:
$Lstfile= #(Get-ChildItem 'G:\BACKUPS\' -Filter *.zip | sort LastWriteTime | select -last 1 )
Error:
Get-ChildItem : The given path's format is not supported.
At C:\DailyBackup.ps1:101 char:13
+ $Lstfile= #(Get-ChildItem "G:\BACKUPS\" -Filter *.zip | sort LastWrit ...
+ CategoryInfo : NotSpecified: (:) [Get-ChildItem], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.GetChildItemCommand
Goal is timestamp of LAST backup file created (with the filter "*.zip"). Tried at least a dozen different ways to derive last modified data, all throw the same (or similar) error "Given path's format is not supported"
And yes, "G:\Backups" does exist.
THANX.

Checking to see if PowerShell Command worked, Crap Error

Needed to clean up a winmail.dat issue by adding a contact and setting a couple of parameters. All worked as shown, except the command to test that it worked.
Get-MailContact | Select randomemail#email.com | Select -UseMapiRichTextFormat
What is the reason for this failure?
Select-Object : A parameter cannot be found that matches parameter
name 'UseMapiRichTextFormat'. At line:1 char:62 + ... t 1 Select
support#solidpe.maxdesk.us 1 Select -UseMapiRichTextFormat +
---------------------- + CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException + FullyQualifiedErrorId :
NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I think what you actually need is this:
Get-MailContact |
Where-Object {$_.ExternalEmailAddress -eq 'randomemail#email.com'} |
Select-Object -Property UseMapiRichTextFormat
Where-Object limits the result set by comparing each contact's ExternalEmailAddress property and only including the objects that match in the output (which should only be one)
Select-Object limits the output object members to a subset of the original members

How to check particular VMware VM is not currently running in the task using PowerShell?

I need to check particular VMware VM is currently in the recent task like CLone_task, Migrate_VMTask,.etc and also skip that VM before VM migration starts..
I have tried the below code:
PS> Get-Task (Get-VM -Name VM1) | Select State
Get-Task : Cannot bind parameter 'Status'. Cannot convert the "nalb00cava3"
value of type "VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl"
to type "VMware.VimAutomation.Sdk.Types.V1.TaskState".
At line:1 char:10
+ Get-Task (Get-VM -Name nalb00cava3) | Select State
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Task], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTask
This should do what you're looking for, the final pipe is where you would define the specific VM.
Get-Task | ?{$_.ObjectId -match 'VirtualMachine'} | Select #{N='VM';E={(Get-View -Id $_.ObjectId).Name }},State,Description | where {$_.VM -eq "VM1"}
It filters on ObjectId from Get-Task, then referencing the Id, determines the VM names, and finally filters on the VM you define.

PowerShell remote event log parsing

I wrote a small script to grab event log entries from a remote machine and write it to a .csv file. The script works when targeting a single machine, but when I try to implement a for loop and loop it over all machines in Active Directory, I get this error:
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADComputer]
does not contain a method named 'op_Addition'.
At Y:\srp.ps1:7 char:143
+ ... | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Export-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty.
Provide an argument that is not null or empty, and then try the command again.
At Y:\srp.ps1:7 char:141
+ ... 0 | Export-Csv $($computer + ".csv")
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Export-Csv],
ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,
Microsoft.PowerShell.Commands.ExportCsvCommand
The error indicates there's a problem with the Export-Csv command, but running the command by itself creates the log files needed. Here is the full script, for reference:
# Gets SRP event log entries from remote machine and writes them to a .csv file
# of the same name.
Write-Output "Running..."
$computers = Get-ADComputer -filter {(Name -like "PC*") -or (Name -like "LT*")}
foreach ($computer in $computers) {
Get-EventLog -LogName Application -Source Microsoft-Windows-SoftwareRestrictionPolicies
-ComputerName $computer -Newest 10 | Export-Csv $($computer + ".csv")
} #end foreach
Write-Host "Done."
Any ideas as to why this error appears when I try to loop over computers in AD?
It looks like Get-ADComputer returns ADComputer objects, but you're passing it to Get-EventLog's ComputerName parameter, which takes a string, as-is. I'm assuming you'll need to grab the name property from the Microsoft.ActiveDirectory.Management.ADComputer object.

PowerShell: combining paths using a variable

This must be something obvious, but I can't get this to work.
I'm trying to build a variable that should contain the path to an existing file, using an environment variable ($env:programfiles(x86)). However I keep getting errors, and I fail to see why.
This works fine (if the file exists):
PS C:\> $f = "C:\Program Files (x86)" + '\sometextfile.txt'
PS C:\> $f
C:\Program Files (x86)\sometextfile.txt
PS C:\> gci $f
Directory: C:\Program Files (x86)
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 13/12/2010 14:03 0 sometextfile.txt
PS C:\>
However, this does not:
PS C:\> "$env:programfiles(x86)"
C:\Program Files(x86)
PS C:\> $f = "$env:ProgramFiles(x86)" + '\sometextfile.txt'
PS C:\> $f
C:\Program Files(x86)\sometextfile.txt
PS C:\> gci $f
Get-ChildItem : Cannot find path 'C:\Program Files(x86)\sometextfile.txt' because it does not exist.
At line:1 char:4
+ gci <<<< $f
+ CategoryInfo : ObjectNotFound: (C:\Program Files(x86)\sometextfile.txt:String) [Get-ChildItem], ItemNot
FoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
What's happening, and how do I fix it?
Here is what is going on...
In any Windows PowerShell path empty characters or spaces need to be surrounded with a set of quotes or brackets. The PowerShell environment variable for the C:\Program Files (x86) is ${env:ProgramFiles(x86)}, not $env:ProgamFiles(x86) since PowerShell needs to escape the empty spaces in the real path.
If you use the '${env:ProgramFiles(x86)}' explicit environment variable, it works perfectly.
This won't work...
PS C:\> cd "$env:programfiles(x86)"
Set-Location : Cannot find path 'C:\Program Files(x86)' because it does not e
At line:1 char:3
+ cd <<<< "$env:programfiles(x86)"
+ CategoryInfo : ObjectNotFound: (C:\(x86):String)
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.
or this...
PS C:\> $env:ProgramFiles(x86)
Unexpected token '(' in expression or statement.
At line:1 char:19
+ $env:ProgramFiles( <<<< x86)
+ CategoryInfo : ParserError: ((:String) [], Parent
+ FullyQualifiedErrorId : UnexpectedToken
But this works great...
PS C:\> ${env:ProgramFiles(x86)}
C:\Program Files (x86)
PS C:\> $f = "${env:ProgramFiles(x86)}" + "\sometextfile.txt"
PS C:\> $f
C:\Program Files (x86)\sometextfile.txt
PS C:\> gci $f
Directory: C:\Program Files (x86)
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 12/13/2010 8:58 AM 0 sometextfile.txt
That is weird and looks like a bug. Actually it is resolving the $env:programfiles variable and appending the rest of the string - which in this case just happens to be (x86).
This will work though:
$f = ${env:ProgramFiles(x86)} + '\sometextfile.txt'