我试图学习Powershell并且根本没有任何脚本语言的经验(好吧,除了批处理)。
我尝试做的是运行此批处理命令 - > IF "%USERNAME%"=="Admin" GOTO Admin
作为Powershell命令,然后重新安装用户默认打印机。我需要goto的原因是因为我想为几个用户使用相同的脚本。这就是我所拥有的Batch / Powershell Hybrid
IF "%USERNAME%"=="Admin" GOTO Admin
:Admin
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\adprinter")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\adprinter")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'adprinter'"
$printer.SetDefaultPrinter()
exit
GOTO eof
我想在powershell中做整件事,但我还是没有得到它。我读到你可以做一个"转到"在PowerShell中使用if循环。例如,我拥有的是什么
$user = [Environment]::UserName
if ($user = Admin) do {""}
但是我现在如何运行命令呢?以及如何为其他用户名执行另一个循环?
感谢您的帮助!
修改
这是我到目前为止的整个脚本。我需要一个" goto"因为我有多个用户,每个用户都需要自己的打印机。将每个Batch命令转换为powershell命令会很好。
@echo off
IF "%USERNAME%"=="user1" GOTO user1
IF "%USERNAME%"=="user2" GOTO user2
IF "%USERNAME%"=="user3" GOTO user3
IF "%USERNAME%"=="user4" GOTO user4
IF "%USERNAME%"=="user5" GOTO user5
:user1
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\printer1")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\printer1")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'printer1'"
$printer.SetDefaultPrinter()
exit
GOTO eof
:user2
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\printer2")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\printer2")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'printer2'"
$printer.SetDefaultPrinter()
exit
GOTO eof
:user3
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\printer3")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\printer3")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'printer3'"
$printer.SetDefaultPrinter()
exit
GOTO eof
:user4
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\printer4")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\printer4")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'printer4'"
$printer.SetDefaultPrinter()
exit
GOTO eof
:user5
powershell
(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\Server\printer5")
(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\Server\printer5")
$printer = Get-WmiObject -Query "Select * from Win32_Printer Where ShareName = 'printer5'"
$printer.SetDefaultPrinter()
exit
GOTO eof
:eof
exit
答案 0 :(得分:1)
我同意arco我喜欢使用HashTables,你的问题非常适合它。看看我的代码,它应该给你一个关于Powershell Start的好点。
$drac = @{ }
Import-Csv \\RemoteServer\Share\Folder\Powershell-scripts\Connect-rac.CSV | ForEach-Object {
$drac[$_.ServerName] = $_.IMM
}
如果你看一下上面这就是我的CSV文件并将其加载到哈希表中的原因
及以下,如果我的代码如何使用,我会查找计算机名称,如果匹配则它将返回哈希表值。
if ($drac.Contains($ComputerName))
{
Write-Host "Servername : $ComputerName"
$URL = $drac[$ComputerName]
$IE = New-Object -COM 'InternetExplorer.Application'
$IE.Navigate2("http:\\$URL")
$IE.Visible = $true
}
看看这对你有帮助。