Eu tenho um script de automação que estou escrevendo e, dentro dele, estou registrando ações em um arquivo de texto. Eu gerei o nome $logFile com:
$logFile = "\\server\e$\LogPath\log-$(Get-Date -Format 'yyMMdd-HHmmss').log"
Em seguida, escrevo no arquivo de log usando esta sintaxe:
"Beginning migrations..." | Out-File -FilePath $logFile
Isso funciona bem e posso ver a saída no arquivo de log resultante. No entanto, depois de configurar tudo, entro em um loop foreach para fazer o trabalho real e registro o que está acontecendo, assim:
foreach ($system in $systemList) {
if ($address = Resolve-DnsName -Name $system) {
"test" | Out-File FilePath $logFile -Append
"Hostname $system resolves to $($address.IPAddress -join ',')" | Out-File FilePath $logFile -Append
}
}
Neste ponto do script, ele começa a reclamar sobre a codificação da string do nome do arquivo:
Out-File : Cannot validate argument on parameter 'Encoding'. The argument "\\server\e$\LogPath\log-180719-101053.log" does not belong to the set "unknown,string,unicode,bigendianunicode,utf8,utf7,utf32,ascii,default,oem" specified by the
ValidateSet attribute. Supply an argument that is in the set and then try the command again.
At C:\users\username\SharePoint\Site\Path\script.ps1:133 char:32
+ "test" | Out-File FilePath $logFile -Append
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.OutFileCommand**strong text**
Alguém pode me dizer o que estou fazendo de errado e como corrigir isso?
Você está ausente
-
antesFilePath
de , portanto,FilePath
como a string é vinculada ao primeiro parâmetro posicional, enquanto$logfile
é vinculada ao segundo parâmetro posicional, que éEncoding
.