使用其他用户的凭据启动 powershell 脚本不起作用
Start powershell script with credentials of other user isn't working
我尝试使用 -credential
参数 运行 具有更高权限的脚本
当我尝试使用这部分代码执行此操作时,它显示错误,这基本上意味着 目录名称不正确。 两个脚本(包含 start-process
和change_permissions.ps1
与 \srv1\Projekty\nowy_folder
.
位于同一个文件夹中
没有 -credential
参数脚本完全可以正常工作。
完整脚本如下:
#gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form_start = New-Object System.Windows.Forms.Form
$form_start.Text = 'Tworzenie projektu'
$form_start.Size = New-Object System.Drawing.Size(340,175)
$form_start.StartPosition = 'CenterScreen'
$permission_button = New-Object System.Windows.Forms.Button
$permission_button.Location = New-Object System.Drawing.Point(95,25)
$permission_button.Size = New-Object System.Drawing.Size(135,23)
$permission_button.Text = 'Przydziel uprawnienia'
$permission_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Invoke-Command -FilePath "\srv1\nowy_folder\change_permissions.ps1" -Credential $credentials
Start-Process powershell -argumentlist '.\change_permissions.ps1' -workingdirectory "S:" #-Credential ($credentials)
# Using UNC instead of .\change_permissions.ps1 isn't working
})
$form_start.Controls.Add($permission_button)
#Code below is basically the same but runs other script which also isn't running properly
$create_folders_button = New-Object System.Windows.Forms.Button
$create_folders_button.Location = New-Object System.Drawing.Point(110,75)
$create_folders_button.Size = New-Object System.Drawing.Size(100,23)
$create_folders_button.Text = 'Utwórz katalogi'
$create_folders_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process powershell -argument "C:\Projekty\nowy_folder\make_project_folder_with_subfolders.ps1" -Credential ($credentials)
# $form_start.close()
})
$form_start.Controls.Add($create_folders_button)
$form_start.Topmost = $true
$form_start.ShowDialog()
正如您可能注意到的,我也尝试了 invoke-command
。使用 invoke-command
时出现另一个错误:
你有办法解决我的问题吗?
关于这种方法
Invoke-Command -FilePath c:\scripts\test.ps1 -ComputerName Server01
FilePath 参数指定位于本地计算机上的脚本。脚本在远程计算机上运行,结果返回到本地计算机。
如果你的文件是本地保存的,那你可以试试:
Invoke-Command -FilePath $localPath -computername srv1 -Credential $credentials
或者您可以通过这种方法使用完整路径:
Invoke-Command -computername srv1 -scriptblock { \srv1\nowy_folder\change_permissions.ps1} -Credential $credentials
或者您可以使用直接路径
Invoke-Command -computername srv1 -scriptblock { c:\scripts\change_permissions.ps1} -Credential $credentials
我尝试使用 -credential
参数 运行 具有更高权限的脚本
当我尝试使用这部分代码执行此操作时,它显示错误,这基本上意味着 目录名称不正确。 两个脚本(包含 start-process
和change_permissions.ps1
与 \srv1\Projekty\nowy_folder
.
没有 -credential
参数脚本完全可以正常工作。
完整脚本如下:
#gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form_start = New-Object System.Windows.Forms.Form
$form_start.Text = 'Tworzenie projektu'
$form_start.Size = New-Object System.Drawing.Size(340,175)
$form_start.StartPosition = 'CenterScreen'
$permission_button = New-Object System.Windows.Forms.Button
$permission_button.Location = New-Object System.Drawing.Point(95,25)
$permission_button.Size = New-Object System.Drawing.Size(135,23)
$permission_button.Text = 'Przydziel uprawnienia'
$permission_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Invoke-Command -FilePath "\srv1\nowy_folder\change_permissions.ps1" -Credential $credentials
Start-Process powershell -argumentlist '.\change_permissions.ps1' -workingdirectory "S:" #-Credential ($credentials)
# Using UNC instead of .\change_permissions.ps1 isn't working
})
$form_start.Controls.Add($permission_button)
#Code below is basically the same but runs other script which also isn't running properly
$create_folders_button = New-Object System.Windows.Forms.Button
$create_folders_button.Location = New-Object System.Drawing.Point(110,75)
$create_folders_button.Size = New-Object System.Drawing.Size(100,23)
$create_folders_button.Text = 'Utwórz katalogi'
$create_folders_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process powershell -argument "C:\Projekty\nowy_folder\make_project_folder_with_subfolders.ps1" -Credential ($credentials)
# $form_start.close()
})
$form_start.Controls.Add($create_folders_button)
$form_start.Topmost = $true
$form_start.ShowDialog()
正如您可能注意到的,我也尝试了 invoke-command
。使用 invoke-command
时出现另一个错误:
你有办法解决我的问题吗?
关于这种方法
Invoke-Command -FilePath c:\scripts\test.ps1 -ComputerName Server01
FilePath 参数指定位于本地计算机上的脚本。脚本在远程计算机上运行,结果返回到本地计算机。
如果你的文件是本地保存的,那你可以试试:
Invoke-Command -FilePath $localPath -computername srv1 -Credential $credentials
或者您可以通过这种方法使用完整路径:
Invoke-Command -computername srv1 -scriptblock { \srv1\nowy_folder\change_permissions.ps1} -Credential $credentials
或者您可以使用直接路径
Invoke-Command -computername srv1 -scriptblock { c:\scripts\change_permissions.ps1} -Credential $credentials