How to use PowerShell in a FOR batch file command?

I am trying to create a date string for an LDAP query compatible with the Active Directory whenChanged . I pull AD data into CSV using CSVDE and you need an LDAP query that will filter the results for those items that have changed over the last 2 days. I put together the following FOR command to generate the first part of the comparison string based on several examples found here in Stack Overflow:

 FOR /F "usebackq" %i in (`PowerShell $date^= [DateTime]::Today.AddDays^(-2^)^; $date.ToString^('yyyyMMdd'^)`) DO SET daysAgo = %i 

This FOR command works fine from the command line, but the bombs inside the script package, with the output:

 :Today.AddDays(-2); was unexpected at this time. 

What makes a team bomb? Thanks.

+4
source share
1 answer

I steal its juice ... FOR iterator variables in a batch file must have a double percent sign, %% . So your line will look like this:

 FOR /F "usebackq" %%i in (<snipped-powershell-command>) DO SET daysAgo=%%i 
+2
source

Source: https://habr.com/ru/post/1479463/


All Articles