mardi 6 novembre 2018

Powershell if statement doesn't work when checking for file content

i want to check if C:\test.txt exists or if "Done" String is present in that file, if not, set domain policy, create file C:\test.txt and write "Done" to it.

If i use both conditions:

if (!(Select-String -Path c:\test.txt -Pattern "Done") -or !(Test-Path C:\log.txt)) {Set-ADDefaultDomainPasswordPolicy -Identity ad.contoso.com -ComplexityEnabled $true -MinPasswordLength 7 -MinPasswordAge 1 -MaxPasswordAge 30 -LockoutDuration 00:30:00 -LockoutObservationWindow 00:30:00 -LockoutThreshold 3;write-output "test" | out-file C:\Users\tfl.AD\Desktop\test.txt -Append}

then getting:

Select-String : Cannot find path 'C:\test.txt' because it does not exist.


Set-ADDefaultDomainPasswordPolicy has no any output, that's why i added second command to write `Done` to `log.txt`

Aucun commentaire:

Enregistrer un commentaire