Running a PowerShell script as administrator without typing in passwords

后端 未结 2 1764
忘掉有多难
忘掉有多难 2020-12-18 14:27

I wrote a script that will switch between having a computer connect via Wi-Fi or wired Internet simply by running a batch file. I wrote this because I don\'t like having to

2条回答
  •  庸人自扰
    2020-12-18 14:53

    You can start a new, elevated PowerShell process to run your script e.g.:

    Start-Process PowerShell -verb runas -ArgumentList '-noexit','-File','path-to-script'
    

    If you don't want the PowerShell window to hang around then get rid of the '-noexit' but for debugging the launch of your script, it is useful.

    If you had access to an admin account username/password, you could do this:

    # Capture encrypted password once and store to file
    $passwd = Read-Host "Enter password" -AsSecureString
    $encpwd = ConvertFrom-SecureString $passwd
    $encpwd > $path\password.bin
    
    # Afterwards always use this to start the script
    $encpwd = Get-Content $path\password.bin
    $passwd = ConvertTo-SecureString $encpwd
    $cred = new-object System.Management.Automation.PSCredential 'domain\username',$passwd
    Start-Process PowerShell -Cred $cred -ArgumentList '-noexit','-File','path-to-script'   
    

提交回复
热议问题