我的脚本中有一个奇怪的问题.我正在某些服务器上调用命令.如果我无法连接到服务器(因为它是脱机的或其他东西)我仍然想记录(服务器脱机> log.txt)
显然,当Try块中发生错误时,Catch不执行该块.
为了测试这个,我给ComputerName不存在的参数写了一个值.这是我的代码:
Try {
Invoke-Command -ComputerName hui -ScriptBlock $sb
}
Catch {
Write-Host "Hello"
}
Run Code Online (Sandbox Code Playgroud)
但是脚本从未发出"你好"
我得到的例外是PSRemotingTransportException.我也试着指定异常,也没用
当我在该Invoke-Command行设置断点时,它也会忽略catch块.为什么?
我试图将变量的内容添加到数组中.不知道数组是否已初始化,我使用前面的数据插入逗号以强制它作为数组进行求值.似乎在执行此操作时,使用前面的逗号到您的实际项目,该indexof方法不会为数组中的第一个项目提供正确的索引.
在下面的代码示例中,$a展示了问题,同时$b工作正常; 唯一的区别是初始化时前面的逗号$a.
这是一个错误,还是我错过了什么?
PS C:\code> $a = (,4,5,6)
PS C:\code> $b = (4,5,6)
PS C:\code> $a
4
5
6
PS C:\code> $a.indexof(4)
-1
PS C:\code> $a.indexof(5)
1
PS C:\code> $a.indexof(6)
2
PS C:\code> $b
4
5
6
PS C:\code> $b.indexof(4)
0
PS C:\code> $b.indexof(5)
1
PS C:\code> $b.indexof(6)
2
Run Code Online (Sandbox Code Playgroud)
因为有问题,我只是用我的变量作为前缀[array]并删除了前缀逗号,一切似乎都很好.我只是好奇发生了什么事.
我导入了一个csv文件
$infile = import-csv .\myfile.csv
Run Code Online (Sandbox Code Playgroud)
现在我可以通过显示列(现在是对象属性)
$infile | gm -type NoteProperty
Name MemberType
------- ----------
Column1 NoteProperty
Column2 NoteProperty
etc...
Run Code Online (Sandbox Code Playgroud)
我想要做的是获取Name(Column1..)变量的值.
我安装了xWebAdministration模块.出于某种原因,我仍然收到此错误消息
术语"xWebsite"未被识别为cmdlet的名称"

image url:http://i.stack.imgur.com/tTwUe.jpg
这是我的代码.
Configuration MvcWebTest {
Param(
[String[]]$ComputerName = "tvw-irwebsvc",
$AppName = "MvcWebTest",
$User = "PAOMSvc",
$Password = "Welcome1",
$CodePath = "C:\websites\MvcWebTest"
)
Import-DscResource -Module xWebAdministration
Node $ComputerName {
#Install ASP.NET 4.5
WindowsFeature ASP {
Ensure = “Present”
Name = “Web-Asp-Net45”
}
File WebContent {
Ensure ="Present";
SourcePath ="\\DVW-MORBAM01\Build\Publish\MvcWebTest\Dev";
DestinationPath=$CodePath;
Type = "Directory";
Recurse = $True
}
# Create a new website
xWebsite Website {
Ensure = "Present";
Name = $AppName;
State = …Run Code Online (Sandbox Code Playgroud) 我正在调用start-process命令传递一组值...
$args = @{
FilePath = "c:\myapp.exe"
RedirectStandardOutput = "c:\output.txt"
};
start-process @args;
Run Code Online (Sandbox Code Playgroud)
...这工作正常,但我现在想要将Wait标志添加到start-process的参数中,但是当它不需要赋值给它时,如何将它添加到$ args数组中...
$args = @{
Wait = ?
FilePath = "c:\myapp.exe"
RedirectStandardOutput = "c:\output.txt"
};
Run Code Online (Sandbox Code Playgroud)
我想坚持将参数作为数组传递,因为它们可以传递给一个例程,这个参数很适合我参与进程.
我编写了以下 PS 脚本来从特定服务器路径中删除日志文件。我是 PS 的新手,但是我在这个脚本中编写的一些函数出现了一些错误:
#* FileName: FileCleaner.ps1
#Clear the screen
Clear
#Read XML Config File to get settings
[xml]$configfile = Get-Content "C:\Users\pmcma\Documents\Projects\Replace FileCleaner with PowerShell Script\FileCleaner.config.xml"
#Declare and set variables from Config values
$hostServer = $configfile.Settings.HostServer
$dirs = @($configfile.Settings.DirectoryName.Split(",").Trim())
$scanSubDirectories = $configfile.Settings.ScanSubDirectories
$deleteAllFiles = $configfile.Settings.deleteAllFiles
$fileTypesToDelete = @($configfile.Settings.FileTypesToDelete.Split(";").Trim())
$liveSiteLogs = $configfile.Settings.LiveSiteLogs
$fileExclusions = @($configfile.Settings.FileExclusions.Split(";").Trim())
$retentionPeriod = $configfile.Settings.RetentionPeriod
$AICLogs = $configfile.Settings.AICLogs
$AICLogsRententionPeriod = $configfile.Settings.AICLogsRententionPeriod
$fileCleanerLogs = $configfile.Settings.FileCleanerLogs
$fileCleanerLogsRententionPeriod = $configfile.Settings.FileCleanerLogsRententionPeriod
#Setup FileCleaner output success logfiles
$successLogfile = $configfile.Settings.SuccessOutputLogfile
$dirName = …Run Code Online (Sandbox Code Playgroud) 有没有更好的方法来写这个:
for($j =0; $j -lt 11; $j++){
if($averagetime[$j] -ne "no data"){
Write-host $type[$j] "(" $1st[$j] "):" $averagetime[$j]
}
}
Run Code Online (Sandbox Code Playgroud)
我的outptut看起来像这样: Multiaxial_Milling ( 5 ): 3.46
我希望它像这样输出: Multiaxial_Milling (5): 3.46
我知道${variable}有效,但它不适用于数组.目前这对我来说只是一个美容问题,但我想这可能是未来的一个问题.
$var1 = '06/10/2015 2:00 AM'
$var2 = ([DateTime]($var1)).addMinutes(15).ToString('mm/dd/yyyy hh:mm')
$var2
Run Code Online (Sandbox Code Playgroud)
电流输出 - 15/10/2015 02:15
但我想要 - 06/10/2015 02:15
我正在尝试创建一个包含 3 列的哈希表。
SERVER_NAME PROCESS_NAME SERVER_STATUS PROCESS_AVAILABLE
SERVER1 app1.exe RUNNING YES
SERVER1 app2.exe RUNNING NO
SERVER2 app1.exe OFFLINE NO
SERVER2 app2.exe OFFLINE NO
SERVER3 app1.exe RUNNING YES
SERVER3 app2.exe RUNNING YES
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过这个
$SERVERLIST = Get-Content "$PSScriptRoot\servers\serverManager.bin"
$PROCESSMONITOR = Get-Content "$PSScriptRoot\process\application.bin"
$testList = @{Name=$SERVERLIST;Process=$PROCESSMONITOR}
Run Code Online (Sandbox Code Playgroud)
服务器列表在“serverManager.bin”文件中。这是一个包含服务器列表的 CSV 文件。
我有兴趣监控的进程列表在“application.bin”文件中。这是一个包含应用程序列表的 CSV 文件(如 PowerShell 所见)。[见下面的代码]
Get-Process -ComputerName $server -name $process -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)
我想构建一个报告,告诉管理员哪个服务器正在运行以及哪个进程正在从我们感兴趣的列表中运行。
我可以检查进程是否正在运行 我可以检查服务器是否在线
我的问题是我需要做什么才能获得上面发布的输出
我知道标题是满口的,但我不知道如何使它更简洁.
在Perl中读取多行字符串我在PerlMaven上发现了关于here-documents的这篇文章.它谈论这里 - 文件,qq和q.在所有情况下,保留前导空格,如用于代码缩进的空格.我明白那个.在示例节目中减轻这种情况的方法是使用正则表达式替换来删除前导空格.
Run Code Online (Sandbox Code Playgroud)if ($send) { (my $message = qq{ Dear $name, this is a message I plan to send to you. regards the Perl Maven }) =~ s/^ {8}//mg; print $message; }
当我试图采用这种风格时,我就这样写了(不小心):
if ($send) {
my $message = (qq{
Dear $name,
words and stuff
}) =~ s/^ {8}//mg;
print $message;
}
Run Code Online (Sandbox Code Playgroud)
Perl对我来说不是一种强硬的语言.我上面尝试的错误语法对我来说似乎很自然.由于我错误地使用匹配运算符,我显然得到错误:
无法在nagios_send_html_service_mail.pl第91行修改字符串替换(s ///),靠近"s/^ {8} // mg;"
在该工作例子,为什么不将$message实际包含的变化?当你声明变量时我似乎被允许做的事情,但我只是想知道它被称为什么.