I have the following code snippet:
foreach($File in $(Get-ChildItem -Path $FromPath)){ $ObjFolder.CopyHere($File.fullname, $CopyOptions); }
It copies the files to the windows folder, however the users are not allowed to do this, I'm trying to use the following excerpt:
Invoke-Command -ScriptBlock $script -Credential contoso\admin;
But it is not working, would you have to save the credentials in a variable, so that it does not ask permission for every file that will be copied?
Full script:
function getCredencial(){ $usario = Read-Host "Usuario" $senha = Read-Host -AsSecureString "Senha" return New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $usario, $senha } $script = { $FONTS = 0x14; $FromPath = "\servidor01\arquivos\fontes\"; $ObjShell = New-Object -ComObject Shell.Application; $ObjFolder = $ObjShell.Namespace($FONTS); $CopyOptions = 4 + 16; $CopyFlag = [String]::Format("{0:x}", $CopyOptions); foreach($File in $(Get-ChildItem -Path $FromPath)){ $ObjFolder.CopyHere($File.fullname, $CopyOptions); } } $credenciais = getCredencial; Invoke-Command -ScriptBlock $script -Credential $credenciais;