小编Sun*_*une的帖子

CSV的格式不正确?

我在Powershell中生成带有EXPORT-CSV的CSV,然后将其提供给Perl脚本.但Perl无法导入该文件.

我已经针对工作版本(已经从相同的Perl脚本而不是PowerShell导出)验证了CSV文件,并且没有区别.coloumns完全相同,它们都有分号作为分隔符.如果我在Excel中打开文件,但所有内容都会在每行的第一个单元格中结束(这意味着我必须进行文本到颜色).工作文件从一开始就在不同的单元格中结束.

更令人困惑的是:当我在记事本中打开文件并将内容复制/粘贴到新文件时,导入工作正常!

那么,我错过了什么?是否有"隐藏"属性,我无法用记事本发现?我是否必须更改编码类型?

请帮忙:)

csv excel powershell perl export-to-csv

4
推荐指数
1
解决办法
1361
查看次数

使用Start-Job同时运行多个脚本块(而不是循环)

大家好!

我一直在寻找一种方法来提高我的脚本效率,我得出结论(在StackOverflow上的好人帮助下),Start-Job是要走的路.

我有以下foreach-loop,我想在$ servers中的所有服务器上同时运行.我在理解如何实际收集从Receive-Job返回的信息并添加到$ serverlist时遇到问题.

PS:我知道我远远没有把这个钉死,但我真的很感激一些帮助开始,因为我对Start-Job和Receive-Job如何工作感到非常难过.

# List 4 servers (for testing)
$servers = Get-QADComputer -sizelimit 4 -WarningAction SilentlyContinue -OSName *server*,*hyper*

# Create list
$serverlistlist = @()

# Loop servers
foreach($server in $servers) {

    # Fetch IP
    $ipaddress = [System.Net.Dns]::GetHostAddresses($Server.name)| select-object IPAddressToString -expandproperty IPAddressToString

    # Gather OSName through WMI
    $OSName = (Get-WmiObject Win32_OperatingSystem -ComputerName $server.name ).caption

    # Ping the server
    if (Test-Connection -ComputerName $server.name -count 1 -Quiet ) {
        $reachable = "Yes"
    }

    # Save info about server
    $serverInfo = …
Run Code Online (Sandbox Code Playgroud)

powershell foreach jobs

4
推荐指数
1
解决办法
1万
查看次数

即使长度为1,也返回正确的.length

如果我做了

$services = Get-Service
$services.length
Run Code Online (Sandbox Code Playgroud)

我得到126项服务.但是,如果我指定一项服务

$services = Get-Service -Name vds
$services.length
Run Code Online (Sandbox Code Playgroud)

我一无所获.这弄乱了我在脚本中做的循环.即使长度为1,如何从.length返回正确的数字?

使用普通字符串这很容易,我只需指定为数组:

$myarray = ,"one","two","three"
$myarray.length
Run Code Online (Sandbox Code Playgroud)

将返回3,和

$myarray = ,"one"
$myarray.length
Run Code Online (Sandbox Code Playgroud)

将返回1 ..

如何获得正确的长度?

powershell

2
推荐指数
1
解决办法
3540
查看次数

New-Object不能包含破折号( - )但是"需要"

我正在为CSV导出值创建一个新对象:

New-Object -TypeName PSObject -Property @{
            host_name = ($server.name).ToLower()
            address = $IPAddress
            host_is_collector = "no"
            host-preset = "windows-server"
        } | Select-Object host_name,address,host-preset | Export-Csv -Path $nConf_import_host_file
Run Code Online (Sandbox Code Playgroud)

问题是其中一行包含破折号(主机预设).我只需将其更改为下划线,但我的CSV需要此值为破折号.我也可以在创建它之后对整个csv做一个替换 - 但这看起来很脏.有没有办法在这里使用破折号?

我的错误信息是:

Missing '=' operator after key in hash literal.
At Z:\Scripts\Testscripts\ScanServers_and_check_nagiosV7.ps1:336 char:16
+                 host-preset <<<<  = "windows-server"
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral
Run Code Online (Sandbox Code Playgroud)

powershell export-to-csv

1
推荐指数
1
解决办法
2924
查看次数

发送邮件消息失败,因为附件丢失(混乱变量)

我发送了一堆文件作为附件,Send-MailMessage 有些文件存在,有些文件没有,因此Send-MailMessage如果缺少其中一个文件就会失败.

如果文件丢失,有没有办法可以Test-Path清理$attachments

我的代码:

$Attachments = "$longlist_file","$PingList_File","$quicklist_file", `
  "$nConf_import_host_file","$nConf_import_service_file", `
  "$nconf_export_host_file","$nconf_export_service_file"

# Sending mail
send-mailmessage -to $ToAddress -from $FromAddress -smtpserver $SMTPServer `
  -subject $MessageSubject -Body $MessageBody -Attachments $Attachments
Run Code Online (Sandbox Code Playgroud)

variables powershell

1
推荐指数
1
解决办法
3277
查看次数

尝试,捕获:如果TRY成功完成,做一些事情

我循环遍历samaccountnames列表并执行几个操作:

# Disabling user
try {    
    Disable-QADUser $user | Out-Null
} catch [exception] {
    "Disable-QADuser: " + $($_.Exception.Message) | out-file $logfile -append
    write-host " - Error disabling useraccount." -fore yellow
}

# Set informative description
try {    
    Set-QADuser $user -Description "Disabled $now"  | Out-Null
} catch [exception] {
    "Set-QADuser: " + $($_.Exception.Message)| out-file $logfile -append
    write-host " - Error setting informative description in AD." -fore yellow
} 
Run Code Online (Sandbox Code Playgroud)

但是如果命令成功完成,我该如何输出?就像是

write-host "User $user disabled"
"User $user disabled" | out-file $logfile -append
Run Code Online (Sandbox Code Playgroud)

所有的帮助/指针非常感谢! …

powershell try-catch

1
推荐指数
1
解决办法
3227
查看次数

从变量中过滤输出(where-object)

我正在使用以下行在服务器上运行测试:

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter "State='Running'" |
where-object ??? }| Foreach-Object {
                New-Object -TypeName PSObject -Property @{
                    DisplayName=$_.DisplayName
                    State=$_.State
                } | Select-Object DisplayName,State
            # Export all info to CSV
            } | ft -AutoSize
Run Code Online (Sandbox Code Playgroud)

我想创建一个这样的变量:

$IgnoreServices = '"Wireless Configuration","Telephony","Secondary Logon"
Run Code Online (Sandbox Code Playgroud)

并将其发送到Where-Object.我可以这样做吗?

淑娜:)

编辑:经过一些R/T(研究和尝试:))我发现我可以这样做:

$IgnoreServices = {$_.DisplayName -ne "Wireless Configuration" 
-and $_.DisplayName -ne "Telephony" -and $_.DisplayName -ne "Secondary Logon" 
-and $_.DisplayName -ne "Windows Event Collector"}

Get-WmiObject Win32_Service -ComputerName "myserver" -Filter   "State='Running'"|        where-object $IgnoreServices | Foreach-Object {
                # Set new objects for …
Run Code Online (Sandbox Code Playgroud)

powershell wmi active-directory where-clause

0
推荐指数
1
解决办法
5627
查看次数

将带有属性的变量传递给argumentlist,丢失属性

$Computers = Get-QADComputer -sizelimit 5
Run Code Online (Sandbox Code Playgroud)

返回五台计算机的列表.我循环

foreach($computer in $computers) {
echo "and then I can do this $computer.name"
Run Code Online (Sandbox Code Playgroud)

从$计算机中只获取计算机名称.但是当我试图将它传递给这样的开始工作时:

    Start-Job -FilePath $ScriptFile -Name $Computer.Name -ArgumentList $Computer
Run Code Online (Sandbox Code Playgroud)

我无法在$ scriptfile中执行$ computer.name.我必须像$ computer.name一样传递它并将其称为$ args [0].但后来我松开了所有其他属性(我在$ scriptfile中使用了一堆.)

我没有到这里来的是什么?你会叫什么电脑?那你叫什么叫$ computer.name?

淑娜:)

powershell arguments start-job

0
推荐指数
1
解决办法
1838
查看次数