Powershell脚本以管理员

时间:2017-11-06 21:50:54

标签: admin administrator

我有一个脚本可以双击,它会以管理员身份打开其他脚本。适用于某些事情但不是一切。对于一个脚本,它会打开下一个窗口,然后立即关闭它。另一方面,我收到了这个错误:

在MYPATH \ InstallClient.ps1:33 char:78 + ... tall_x64.msi“-force -recurse -ErrorAction停止#清除文件... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ 该字符串缺少终结符:“。 在MYPATH \ InstallClient.ps1:27 char:31 + ForEach($计算机中的$ entry){#start of foreach循环 +〜 在语句块或类型定义中缺少关闭'}'。     + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException     + FullyQualifiedErrorId:TerminatorExpectedAtEndOfString

以下是以管理员身份打开脚本的脚本:

Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "PS1 (*.ps1)| *.ps1"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$inputfile = Get-FileName "MYPATH\Scripts"
powershell.exe -noprofile -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file $inputfile' -verb RunAs}" 

这是在尝试打开时提供上一个错误的脚本:

Function Get-FileName($initialDirectory) #Function to choose a file
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "MSI (*.msi)| *.msi" #type of files that will be available for selection
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}
$inputfile = Get-FileName "MyPath" #Directory that is going to open to select a file from

Function Get-FileName($initialDirectory) #Function to choose a file
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "CSV (*.csv)| *.csv" #type of files that will be available for selection
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}
$inputfile1 = Get-FileName "MyPath\ServerLists"
$computers = import-csv $inputfile1

ForEach ($entry in $computers){ #start of foreach loop
$computername = $entry.computernames #this saves the single entry under computernames for each entry in csv file
Copy-item $inputfile -container -recurse \\$computername\C$\windows\temp #this copies the msi file that we selected to the computer entry called from the csv file's temp folder
Invoke-Command -Computername $computername –ScriptBlock {Start-process -Wait "C:\windows\temp\ShadowSuiteClientInstall_x64.msi"} | out-null #This starts the msi file that we just copied and waits for the installation to be completed before moving on
If($?){ #If the last command was successful
Echo "Installed ShadowSuiteClientInstall_x64 on $computername."
Remove-Item "\\$computername\C$\windows\temp\ShadowSuiteClientInstall_x64.msi" -force -recurse -ErrorAction Stop #Cleans out the file we copied into the temp folder
}
}

有没有人有任何想法为什么这会打开一些好的东西但是为这个脚本提供此错误并立即关闭其他脚本而不运行它们?有没有人有更好的方法来浏览脚本并选择一个以管理员身份打开?

1 个答案:

答案 0 :(得分:0)

好的,我想出来了。我将脚本加载到PowerShell ISE中,我发现它正在编译错误。它不断将-Scriptblock转换为ae符号而不是 - 在scriptblock前面 - 。奇怪的自动对焦IMO但是好的,我在ISE中修复了它,我推荐给那些在这类奇怪的编译错误中苦苦挣扎的人。