【问题标题】:Remove a file using powershell through admin account通过管理员帐户使用 powershell 删除文件
【发布时间】:2019-02-26 01:19:30
【问题描述】:

我正在尝试做的事情的简要总结。 我在 powershell 中有一个脚本,它需要 2 个文件并读取嵌入式凭据并将它们存储在一个变量中,然后我可以从中运行管理命令。

这很好用,但是,在读取文件并存储密钥后,我试图删除这 2 个文件,但我不断收到以下错误:

Start-Process : 参数集无法使用指定的解析 命名参数。在 \mars\Client-Installs\NetSmart Test3\Setup.ps1:137 字符:15 + Start-Process

Start-Process : 参数集无法使用指定的解析 命名参数。在 \mars\Client-Installs\NetSmart Test3\Setup.ps1:138 字符:15 + Start-Process

我运行的帐户是域管理员的一部分,当我查看任务管理器时,我可以看到它在管理模式下运行。

我还知道文件所在的文件夹路径也具有完全共享和安全访问权限。

这是我的代码片段(底部 2 行似乎不起作用)

function Authentication
{
    #---------------------------------------------------
    #Authenticate Admin Account using encrypted password
    #---------------------------------------------------
    $TempFolder = $env:temp

    #The 2 lines underneath is if you are running the auth files from the same directory
    #$global:AESKeyFilePath = $path + "\aeskey.txt"
    #$global:SecurePwdFilePath =  $path + "\credpassword.txt"

    #Move the files to the temp folder
    $global:file1 = $path + "\aeskey.txt"
    $global:file2 =  $path + "\credpassword.txt"

    Copy-Item -Path $file1 -Destination $TempFolder -force
    Copy-Item -Path $file2 -Destination $TempFolder -force

    #If you choose to run it from the temp directory comment the lines above and uncomment the 2 below.
    $global:AESKeyFilePath = $TempFolder + "\aeskey.txt"
    $global:SecurePwdFilePath =  $TempFolder + "\credpassword.txt"
    $global:userUPN = "domain\user"

    #use key and password to create local secure passwordtemp
    $global:AESKey = Get-Content -Path $AESKeyFilePath 
    $global:pwdTxt = Get-Content -Path $SecurePwdFilePath
    $global:securePass = $pwdTxt | ConvertTo-SecureString -Key $AESKey

    #create a new psCredential object with required username and password
    $global:adminCreds = New-Object System.Management.Automation.PSCredential($userUPN, $securePass)

    #Remove the files below
    Start-Process -FilePath "powershell.exe" -Credential $adminCreds -WindowStyle Hidden -ArgumentList "Remove-Item -Path $file1 -Force" -WorkingDirectory $path -NoNewWindow -PassThru
    Start-Process -FilePath "powershell.exe" -Credential $adminCreds -WindowStyle Hidden -ArgumentList "Remove-Item -Path $file2 -Force" -WorkingDirectory $path -NoNewWindow -PassThru
}

【问题讨论】:

    标签: powershell admin administrator start-process


    【解决方案1】:

    您不能同时指定-NoNewWindow 和-WindowStyle,这是矛盾的。

    参数集参见Get-Command Start-Process -Syntax。

    我希望以下方式是您所需要的。只需使用-WindowStyle Hidden。

     Start-Process -FilePath "powershell.exe" -Credential $adminCreds -WindowStyle Hidden -ArgumentList "Remove-Item -Path $file2 -Force" -WorkingDirectory $path -PassThru
    

    【讨论】:

    • 我试了一下,但还是没有运气。我尝试修改我的代码并将其放入 try/catch 块中以获取错误消息,输出如下:“无法使用指定的命名参数解析参数集。”
    • 你能分享你在上述建议后尝试的代码吗?
    • 我最终解决了这个问题,但我重写了整个应用程序以使其正常工作。基本上你的语法是正确的,但是在进一步的故障排除后,由于某种原因,如果我从该函数或该脚本中运行“删除项目”,它仍然没有“启动”管理员权限。为了解决这个问题,我创建了一个单独的批处理脚本来执行;只是为了删除这些文件。在我这样做之后,它完全按照预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多