如果该进程未在远程计算机上运行,我正在尝试启动该进程。问题是,如果其中一台远程计算机无法访问,脚本将停止并使用脚本的第一个“ IF”部分。
当我将一台可访问的计算机排在首位时,一切正常。当我首先放置无法访问的计算机时,脚本会启动该过程,而不会检查列表中的第二台计算机。
我以管理员身份使用批处理文件启动脚本:
powershell -Command Set-ExecutionPolicy Bypass -Force
powershell -Command "\\networkpath\bla.ps1"
脚本:
$ErrorActionPreference = 'SilentlyContinue'
$ProcessName = “processname”
$Computername = Get-Content \\networkpath\computernamen.txt |
ForEach-Object { $_ -split ',' }
$ProcessActive = Get-Process $ProcessName -ComputerName $Computername -EV Err -EA SilentlyContinue
if ($ProcessActive -eq $null) {
Start-Process -FilePath "\\networkpath\aktuell.bla"
} else {
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Programm gesperrt.", "Info", 0)
}
答案 0 :(得分:0)
如果我很理解您想要在其他列出的计算机上没有运行另一个(或相同)进程的命令或进程,对吗?
在这种情况下,您可以尝试以下方法:
<#
.SYNOPSIS
Runs a process if another or same process is not running on other computers.
.DESCRIPTION
Search for a process (specified in -Process parameter) on computers (specified in -Computer or -List parameters).
If not any process is found, it runs then another process (specifies in -Run parameter)
.PARAMETER Computer
Specifies one or more computer name
This value(s) can come from the pipeline
.PARAMETER List
Specifies path of a file which contains a list of computer names
The file can contain computer names one per line, or in one line separated by ',' or ';'
.PARAMETER Process
Specifies the name of the process to check on computers
.INPUTS
This script accept from pipeline one or more System.String representing computer names
.OUTPUTS
None
#>
[CmdletBinding(DefaultParameterSetName='Default')]
Param (
# Specifies one or more computer name
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
ParameterSetName='Default')]
[ValidateNotNullOrEmpty()]
[Alias("ComputerName")]
[string[]]
$Computer,
# Specifies path of a computer list file.
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName = 'FromFile')]
[ValidateScript( { Test-Path $_ -PathType Leaf } )]
[Alias("ListFile")]
[string]
$List,
# Specifies the process name
[Parameter(Mandatory=$true,
Position=1)]
[ValidateNotNullOrEmpty()]
[Alias("ProcessName")]
[String]
$Process,
# Specifies the process to run
[Parameter(Mandatory = $true,
Position = 2)]
[ValidateNotNullOrEmpty()]
[Alias("ProcessToRun")]
[String]
$Run
)
# Set default ErrorAction setting
$ErrorActionPreference = 'SilentlyContinue'
# If current Parameter Set is 'FromFile' (-List parameter was used followed by a file path)
If ($PSCmdlet.ParameterSetName -eq 'FromFile') {
# For each line in the provided file...
Get-Content -Path $List | ForEach-Object {
# Test line:
switch ($_) {
# If any ',' is found in the line, then split each value separated by a ',' in Computer array of strings and process next line
{ $_ -match '*,*' } { [string[]]$Computer = $_.Split(',');continue }
# If any ';' is found in the line, then split each value separated by a ';' in Computer array of strings and process next line
{ $_ -match '*;*' } { [string[]]$Computer = $_.Split(';');continue }
# In any other case...
Default {
# If Computer variable does not exists, create an empty array of strings
if (!$Computer) { [string[]]$Computer = @() }
# Add line content as a new item of the Computer array of strings
[string[]]$Computer += , $_
}
}
}
}
# Set a flag to False
$isRunning = $false
# For each computer ($item) in Computer array...
foreach ($item in $Computer) {
# If Get-Process command for provided Process on current computer found something, set flag to True
if (Get-Process -Name $Process -ComputerName $item -ErrorVariable Err) { $isRunning = $true }
}
# If Flag equals True (If the searched Process was already running on any of the listed computers)...
if ($isRunning) {
# Start process provided in -Run parameter
Start-Process -FilePath $Run
# Else (no listed computer seems to run the searched process)
} else {
#To display a message through dialog box
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("Your message","Title", 0) | Out-Null
#To display a message in Console
"Your message" | Out-Host
}
将其复制并保存到PS1文件中,然后建议您键入以了解如何使用它:
Get-Help .\YourScript.ps1 -Full
我做得很快,没有花时间去测试。请让我知道它是否有效。