0

Below is the singe line command I am trying to run from command prompt.

powershell -command "Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter \"sourcename = 'Microsoft-Windows-WindowsUpdateClient'\" -ErrorAction SilentlyContinue | select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} | select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\winupdatelatest.txt" 

Error : Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type "System.DateTime"." At line:1 char:10

  • Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter "sour ...
  •  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    • CategoryInfo : WriteError: (:) [Get-Date], ParameterBindingException
    • FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

Thank for your help

1
  • 2
    So what is the error? Commented Aug 8, 2021 at 15:10

1 Answer 1

0

This is your command, where each line is a new parameter:

powershell -command "Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter "sourcename = 'Microsoft-Windows-WindowsUpdateClient'" -ErrorAction SilentlyContinue |select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} | select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\datelatest.txt" 

Basically, your problem is that because you use " ... " ... " ... " some part of the command is seen as a parameter and other part is not.

The easiest solution by far is to put everything inside a .ps1 file and just use:

powershell -file myfile.ps1 -ExecutionPolicy Unrestricted 

But if you really have to use this command, then use """ as substiture for one ". Your command would become:

powershell -command "Get-Date (Get-WmiObject -Class win32_reliabilityRecords -filter """sourcename = 'Microsoft-Windows-WindowsUpdateClient'""" -ErrorAction SilentlyContinue |select @{LABEL = 'date';EXPRESSION = {$_.ConvertToDateTime($_.timegenerated)}} |select -first 01).date -format yyyyMMdd | Out-File -FilePath C:\temp\datelatest.txt" 
2
  • Thanks, i try above but i am getting below error The string is missing the terminator: ". + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString Commented Aug 8, 2021 at 15:34
  • Corrected my answer. Commented Aug 8, 2021 at 19:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.