run script with admin permission

Asked

Viewed 4,410 times

2

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 users are not allowed to do this, I am trying to use the following excerpt:

Invoke-Command -ScriptBlock $script -Credential contoso\admin;

but it’s not working, you’d have to save your credentials in a variable, so that it doesn’t ask permission for every file that you copy?

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;

2 answers

3

Create a function that prompts the user, password and returns a credential object:

function getCredencial(){
    $usario = Read-Host "Usuario"
    $senha = Read-Host -AsSecureString "Senha"

    return New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $usario, $senha
}

#código principal....
$credencial = getCredencial
Invoke-Command -ScriptBlock $script -Credential $credencial -ComputerName $env:COMPUTERNAME

Read-Host -AsSecureString exchange the characters typed by asterisks this way does not display the password content.

References:

Get FQDN Hostname

Parameter set cannot be resolved using the specified named Parameters

  • tested this solution, but is giving the following error: Invoke-Command: O Conjunto de parâmetros não pode ser resolvido usando os parâmetros nomeados especificados.

  • @Meuchapeu, the content of $script is the foreach? which version of the powershell is using?

  • edited the question, put the script as it is now.

  • @Meuchapeu, in invoke-command add the argument -ComputerName $env:COMPUTERNAME that passes the name of the machine.

  • thank you, it worked!

0


Searching the net I found a solution:

using the command Get-Credential it is possible to do the same thing.

assign credentials to a variable:

$credential = Get-Credendial contoso\admin;

and in the Invoke-Command use my variable:

Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock $script -Credential $credential ;

Obs: using Get-Credential, a dialog box will appear to enter the user and the password, and putting contoso\admin after the Get-Credential, just need to enter the password, because the user field will already be filled.

More information about Get-Credential Here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.