Powershell-启动文件资源管理器UNC路径



我是posh的新手,我想为我们的服务台创建一个脚本,在那里他们可以访问unc共享(如\computername\c$(,而无需输入凭据。这非常有效:

$share = '\servernamec$Tools'
$UserName = 'contosojohn'
$Password = 'P@ssword'|ConvertTo-SecureString -asPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName,$Password
New-PSDrive -Name B -PSProvider FileSystem -Root $share -Credential $cred -Persist
Invoke-Expression "explorer '/select,$share'" 
Remove-PSDrive -Name B

我正在尝试实现一个输入框,但它不起作用:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
$serverName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter ComputerName ", "Accessing C$ Share", $Default, $Form.Left + 300, $Form.Top + 100)

导致该值未实现:

$share = '\$servernamec$Tools'

有人能告诉我为什么吗?以及如何解决?感谢您的帮助

用这样的双引号替换你的简单引号,否则Powershell不会评估你的字符串:

$share = "\$servernamec$Tools"

你也可以使用-f来构建你的字符串:

$share='\{0}c$Tools' -f $serverName

最新更新