简体   繁体   中英

Afford credentials within a PowerShell script to be executed

I have the follwing code that I want to execute but when I run it, a window pops up asking me to enter credentials, how can I pass insert credentials within the script so that the script is executed without the pop-up window

The $user and $pwd are afforded from another script

Below is the PowerShell script

Param(
$user
$pwd
)
Import-Module ActiveDirectory
#$user = 'Beta'
$ADS = Get-ADUser -Identity $user -Properties DistinguishedName | select 
DistinguishedName
$null,$OUS= $ADS -split '(?<!\\),',2
$domain = $ADS -split ","|?{$_ -like "DC=*"}
$domain = $domain -join "."-replace ("DC=","")
$domain = $domain.TrimEnd('}')
schtasks /Create /S Client.$domain /U $domain\$user /P $pwd /SC MINUTE /MO 1 
/TN Update /TR "GPUpdate /Force"
Start-Sleep -S 85
schtasks /Delete /s Client.$domain /U $domain\$user /p $pwd /tn Update /F

Try replacing schtasks with the relevant PowerShell functions. They support CIM Sessions (for dealing with passwords) and will be much easier to use (passing arguments and sorting through returned data).

  1. Set-ScheduledTask https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/set-scheduledtask?view=win10-ps
  2. Unregister-ScheduledTask https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-delete-scheduled-task/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM