如何让PowerShell脚本请求网络凭据时自动预设域名?
PowerShell脚本修改:预设域名并请求网络凭据
需求概述
现有PowerShell脚本可实现PS模块自动安装、最新文件获取功能,需优化以下两点:
- 自动预设域名,用户无需在用户名字段手动输入域名前缀
- 请求网络凭据而非本地凭据,适配共享资源访问和远程脚本执行场景
修改后的完整脚本
Add-Type -Name Window -Namespace Console -MemberDefinition ' [DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);' [VOID] [Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0) <# .NAME Template #> Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = New-Object System.Drawing.Point(400,400) $Form.text = "ToolBox Updater" $Form.StartPosition = "CenterScreen" $Form.TopMost = $true $Form.Width = 520 $Form.Height = 180 $Label = New-Object system.Windows.Forms.Label $Label.text = "Click Start to Update Application and Install Required Modules" $Label.AutoSize = $true $Label.width = 25 $Label.height = 10 $Label.location = New-Object System.Drawing.Point(16,16) $Label.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10) # 预创建进度条,避免控件添加时未定义 $ProgressBar = New-Object System.Windows.Forms.ProgressBar $ProgressBar.Location = New-Object System.Drawing.Point(16, 45) $ProgressBar.Size = New-Object System.Drawing.Size(460, 20) $ProgressBar.Style = "Marquee" $ProgressBar.MarqueeAnimationSpeed = 20 $ProgressBar.Visible = $false $jobScript = { Start-Sleep -Seconds 10 } function LoadStuff { $Label.Text = "Processing ..." $ProgressBar.Visible = $true $job = Start-Job -ScriptBlock $jobScript do { [System.Windows.Forms.Application]::DoEvents() } until ($job.State -eq "Completed") Remove-Job -Job $job -Force $Label.Text = "Modules Successfully Loaded" $ProgressBar.Visible = $false $StartButton.Visible = $false $EndButton.Visible = $true # 检查并安装模块,添加参数避免交互提示 try { Import-Module ActiveDirectory -ErrorAction Stop } catch { Install-Module -Name ActiveDirectory -Force -AllowClobber -Scope CurrentUser } try { Import-Module AzureAD -ErrorAction Stop } catch { Install-Module -Name AzureAD -Force -AllowClobber -Scope CurrentUser } try { Import-Module MSOnline -ErrorAction Stop } catch { Install-Module -Name MSOnline -Force -AllowClobber -Scope CurrentUser } } $StartButton = New-Object System.Windows.Forms.Button $StartButton.Location = New-Object System.Drawing.Point(350, 90) $StartButton.Size = New-Object System.Drawing.Size(60, 25) $StartButton.Text = "Start" $StartButton.Height = 40 $StartButton.Add_Click({ LoadStuff }) $EndButton = New-Object System.Windows.Forms.Button $EndButton.Location = New-Object System.Drawing.Point(350, 90) $EndButton.Size = New-Object System.Drawing.Size(60, 25) $EndButton.Text = "OK" $EndButton.Height = 40 $EndButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $EndButton.Visible = $false $EndButton.Add_Click({ otbx }) # 添加控件,移除未定义的$Button1引用 $Form.Controls.AddRange(@($Label, $ProgressBar, $StartButton, $EndButton)) function otbx { # 替换为实际域名 $domain = "YOUR_DOMAIN" # 弹出凭据窗口,自动填充域名前缀 $credential = Get-Credential -UserName "$domain\" -Message "请输入网络凭据" # 解压共享文件夹中的文件 Expand-Archive -LiteralPath '\\ServerName\Topfolder\Subfolder\newversion.zip' -DestinationPath "C:\local Folder" -Force $Form.Close() Start-Sleep -Seconds 5 # 使用网络凭据启动主脚本 Start-Process -FilePath powershell.exe -ArgumentList "C:\Local Folder\Main script.ps1" -Credential $credential } [void]$Form.ShowDialog()
关键修改说明
- 预设域名的凭据请求:在
otbx函数中,通过Get-Credential -UserName "$domain\"自动在用户名输入框填充域名\前缀,用户仅需输入自身用户名即可 - 网络凭据适配:
Get-Credential弹出的标准凭据窗口默认支持网络身份验证,满足共享资源访问和远程脚本执行的权限需求 - 控件逻辑修复:将进度条创建移到函数外,避免控件添加时的未定义错误;移除原脚本中未定义的
$Button1引用;修正结束按钮的初始可见性设置 - 模块安装优化:在
Install-Module中添加-Force -AllowClobber -Scope CurrentUser参数,避免安装过程中的交互提示和权限问题 - 凭据传递优化:将完整凭据对象传入
Start-Process,替代原脚本中不完整的Domain\写法,确保身份验证正常执行
内容的提问来源于stack exchange,提问作者PCMedicJAX
相关产品推荐
相关产品推荐

