winforms 我怎么能要求凭据,但假设域?

z9ju0rcb  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(108)

我有一个脚本,以确保所需的PS模块已安装,并获得运行脚本所需的所有文件的最新版本。我有什么工作,但我宁愿它“假设”的域,而不是必须在用户名字段中的域。必须预先填充用户名字段是一个nuisanse的用户。我需要它来要求网络凭据,而不是本地凭据。

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)

$jobScript =
{
    Start-Sleep -Seconds 10
}

function LoadStuff {
   $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
    
    $Form.Controls.Add($ProgressBar);

    $Label.Font = $procFont
    #$Label.ForeColor = 'red'
    $Label.Text = "Processing ..."
    $ProgressBar.visible

    $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.Hide()
    $StartButton.Hide()
    $EndButton.Visible

    try {
    Import-Module ActiveDirectory
    #Write-Host "Module exists"
} 
catch {
    Install-Module -Name ActiveDirectory
    }
try {
    Import-Module AzureAD
    Write-Host "Module exists"
} 
catch {
    Install-Module -Name AzureAD
}

try {
    Import-Module MSOnline
    #Write-Host "Module exists"
} 
catch {
    Install-Module -Name MSOnline

  
}}

$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.BackColor = 'red'
#$StartButton.ForeColor = 'white'
$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.BackColor = 'red'
#$EndButton.ForeColor = 'white'
$EndButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$EndButton.Add_click( { otbx })

$Form.controls.AddRange(@($Label,$ProgressBar,$Button1,$StartButton,$EndButton))

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {

}

function otbx {
Expand-Archive -LiteralPath '\\ServerName\Topfolder\Subfolder\newversion.zip' -DestinationPath C:\local Folder -Force
$Form.Close()
Start-Sleep -Seconds 5
Start-Process -FilePath Powershell "C:\Local Folder\Main script.ps1" -Credential Domain\
}

[void]$Form.ShowDialog()
vnzz0bqm

vnzz0bqm1#

我想我现在明白了。我修改了函数以获取当前用户并在末尾添加-ADM

$usr = (Get-WmiObject  –Class Win32_ComputerSystem | Select-Object UserName).Username
Expand-Archive -LiteralPath '\\ServerName\FolderName\SubfolderName\Current_Version_Main_Script.zip' -DestinationPath C:\Local_Folder -Force
$Form.Close()
Start-Process -FilePath Powershell "C:\folder\Main_script.ps1" -Credential $usr-ADM

相关问题