英文:
Powershell 5.1 - Prompt for credentials
问题
问题:在下面的脚本中,如何使得在使用AD时弹出一个提示,要求输入凭证?
我尝试过创建另一个 PowerShell 脚本来获取凭证,然后运行这个脚本,但是在输入凭证后无法启动脚本。我还尝试将凭证插入到 [ADSI] 快捷方式中,但是无法搞清楚怎么做。
我的代码如下:
While($true){
Clear-Host
$IPSet = Ping-IPRange -StartAddress 56.192.87.200 -EndAddress 56.192.87.254 -Interval 60
$IPSet | Select-object IPAddress | foreach {
$IPA = $_.IPAddress
$HN = [System.Net.Dns]::GetHostByAddress($IPA) | Select-Object -ExpandProperty Hostname
$Administrators = invoke-command -computername "$HN" {get-localgroupmember -Name "administrators"}
if ($Administrators -like 'Admin')
{
Write-Host 'Admin is already an administrator'
}
elseif ($Administrators -notlike 'Admin')
{
$AdminGroup = [ADSI]"WinNT://$IPA/Administrators,group"
$User = [ADSI]"WinNT://USA/EAGANACE,user"
$AdminGroup.Add($User.Path)
Write-Host 'Admin has been added as an administrator'
}
}
}
到目前为止,我尝试过的方法:
创建一个脚本来以提升的凭证调用这个脚本:
$Cred = Get-Credential
$ScriptLocation = "文件路径"
Start-Process -Credential $Cred -FilePath "Powershell.exe" -ArgumentList "-file $Scriptlocation"
我还尝试在 [ADSI] 命令中使用 -Credential 参数,但是它不起作用。我读到过可以将凭证添加到 [ADSI] 中,但是无法找出如何做。
非常感谢您的批评和指导!
英文:
Question: How do I get a prompt to pop up asking for credentials when working with AD in the below script?
I have tried creating another powershell script that gathers the credentials to run this one, but it fails to start the script after entering the credentials. I also tried to insert the credentials into the [ADSI] shortcut but wasn't able to figure that out.
My code as it is:
While($true){
Clear-Host
$IPSet = Ping-IPRange -StartAddress 56.192.87.200 -EndAddress 56.192.87.254 -Interval 60
$IPSet | Select-object IPAddress | foreach {
$IPA = $_.IPAddress
$HN = [System.Net.Dns]::GetHostByAddress($IPA) | Select-Object -ExpandProperty Hostname
$Administrators = invoke-command -computername "$HN" {get-localgroupmember -Name "administrators"}
if ($Administrators -like '*Admin*')
{
Write-Host 'Admin is already an administrator'
}
elseif ($Administrators -notlike '*Admin*')
{
$AdminGroup = [ADSI]"WinNT://$IPA/Administrators,group"
$User = [ADSI]"WinNT://USA/EAGANACE,user"
$AdminGroup.Add($User.Path)
Write-Host 'Admin has been added as an administrator'
}
}
}
What I have tried so far:
Creating a script to call this one with elevated credentials:
$Cred = Get-Credential
$ScriptLocation = "filepath"
Start-Process -Credential $Cred -FilePath "Powershell.exe" -ArgumentList "-file $Scriptlocation"
I also tried -Credential on the [ADSI] command, but it does not take that.
I read that you can add credentials to [ADSI] but was unable to figure out how to do so.
Any criticism/guidance is greatly appreciated!
答案1
得分: 0
我弄清楚了如何完成我想要的事情,似乎很容易,并且似乎可以重复运行任何脚本。文件路径并不重要,但为了解释/简化的目的,我将使用C:\Scripts作为我的示例文件路径,而在该文件夹中有我的脚本MyScript.ps1。
我在同一文件夹中创建了MyScript.ps1的快捷方式,然后修改了快捷方式的属性如下:
转到“高级...”选中“以管理员身份运行”的框并点击确定
目标:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe & 'C:\Scripts\MyScript.ps1'
启动位置:
"C:\Scripts\MyScript.ps1"
只需将C:\Scripts\MyScript.ps1替换为您的路径,并在目标和启动位置的引号中保持引号不变。通过在属性页面上单击确定来完成更改,然后双击快捷方式,以提示您输入管理员凭据并在获得授权后启动您的脚本。
如果不起作用,请确保没有拼写错误,并且引号放在正确的位置。只要将myscript.ps1保留在相同的位置,您可以随意移动快捷方式,只要它位于目标位置即可调用myscript.ps1。
编辑:
在Powershell.exe、&符号和'C:\Scripts\MyScripts.ps1'之间有空格。如果遇到问题,最好只需复制并粘贴它,并替换C:\Scripts\Myscripts.ps1部分。记得保持引号不变!
英文:
I figured out how to go about what I wanted done, seems easy and appears to be repeatable for any script you want to run. The file paths don't really matter, but just for explanation/simplicity purposes I will use C:\Scripts as my example filepath and within that folder lies my script MyScript.ps1.
I created a shortcut of MyScript.ps1 in that same folder, then modified the properties of the shortcut as follows:
Go to 'Advanced...' check the box in front of 'Run as administrator' and hit ok
Target:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe & 'C:\Scripts\MyScript.ps1'
Start in:
"C:\Scripts\MyScript.ps1"
Just replace the C:\Scripts\MyScript.ps1 with your path and keep the quotations in both the target and the start in location. Finalize your changes by clicking ok on the properties page and then double click the shortcut for it to prompt you for administrator credentials and start your script once authorized.
If it isn't working make sure you have no spelling errors and you have the quotations in the right spot. As long as you leave the myscript.ps1 in the same location you can move the shortcut around wherever you like and it will still call the myscript.ps1 as long as it lives in that Target: location.
Edit:
There are spaces between Powershell.exe, the &, and 'C:\Scripts\MyScripts.ps1'. Best to just copy and paste it and replace the C:\Scripts\Myscripts.ps1 portion if you are having trouble. Remember to keep the quotation marks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论