gpt4 book ai didi

csv - Powershell Export-Csv提供了不良结果

转载 作者:行者123 更新时间:2023-12-02 23:59:46 26 4
gpt4 key购买 nike

您好,我希望你们能帮助我解决困扰我几天的问题。
将脚本的结果导出到csv文件时,我无法正确输出,得到以下结果。

我所看到的
What I See

我想看什么
enter image description here

函数Get-ScheduledTask
{
[CmdletBinding()]

参数(
[参数(
必填= $ true,
ValueFromPipeline = $ true,
ValueFromPipelineByPropertyName = $ true)]
[String []] $ ComputerName,

[参数(必须的= $ false)]
[String []] $ RunAsUser,

[参数(必须的= $ false)]
[String []] $ TaskName,

[参数(必须的= $ false)]
[alias(“WS”)]
[switch] $ WithSpace
)

开始
{

$脚本:任务= @()
}

处理
{
$ schtask = schtasks.exe / query / s $ ComputerName / V / FO CSV | ConvertFrom-Csv
详细写入“从$ ComputerName获取计划的任务”

如果($ schtask)
{
foreach($ schtask中的$ sch)
{
if($ sch。“以用户身份运行”-匹配“$($ RunAsUser)”-和$ sch.TaskName-匹配“$($ TaskName)”)
{
详细写入“$ Computername($ sch.TaskName).replace('\','')$ sch。'以用户身份运行'“
$ sch | Get-Member -MemberType属性| ForEach-开始{$ hash = @ {}}-处理{
如果($ WithSpace)
{
($ hash。($ _。Name))= $ sch。($ _。Name)
}
其他
{
($ hash。($($ _。Name).replace(“”,“”)))= $ sch。($ _。Name)
}
} -结束 {
$ script:任务+ =(New-Object -TypeName PSObject -Property $ hash)
}
}
}
}
}

结束
{
$ Script:任务
}
}

$ ComputerName =“SE94ABH02”
$ ServiceAccounts =获取内容“D:\ Scripts \ Test-Peter \ Testing \ ServiceAccounts.txt”

$ obj =新对象–TypeName PSObject
$ obj | Add-MemberType-MemberType NoteProperty-Name ServerName-Value $ ComputerName
$ obj | Add-MemberType-MemberType NoteProperty -Name TaskName -Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType-MemberType NoteProperty -Name ScheduledTaskState -Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType -MemberType NoteProperty -Name LogonMode -Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType-MemberType NoteProperty -Name作者-Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType-MemberType NoteProperty -Name RunAsUser -Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType-MemberType NoteProperty -Name ServiceName -Value([string]::Join(“,”,(@())))
$ obj | Add-MemberType-MemberType NoteProperty -Name StartName -Value([string]::Join(“,”,(@())))

$ SCHTSk = Get-ScheduledTask $ ComputerName |哪里对象{$ _。RunAsUser -like“NLKVKF94 *”} |选择TaskName,ScheduledTaskState,LogonMode,Author,RunAsUser

如果($ SCHTSK){

$ TEMP = @()

foreach($ SCHTSK中的$ TskItem){
如果($ TskItem -match“NLKVKF94”){
$ TEMP + = $ TskItem

$ info = @ {
'TaskName'= $ TEMP.TaskName;
'ScheduledTaskState'= $ TEMP.ScheduledTaskState;
'LogonMode'= $ TEMP.LogonMode;
'作者'= $ TEMP.Author;
'RunAsUser'= $ TEMP.RunAsUser
}
}
}

$ tskobj =新对象-TypeName PSObject-属性$ info
$ obj.TaskName + = $ tskobj.TaskName
$ obj.ScheduledTaskState + = $ tskobj.ScheduledTaskState
$ obj.LogonMode + = $ tskobj.LogonMode
$ obj.Author + = $ tskobj.Author
$ obj.RunAsUser + = $ tskobj.RunAsUser
}


$ WmiObjectResultaat = Get-WmiObject-类win32_service-计算机$ ComputerName |选择对象__SERVER,Name,StartName

如果($ WmiObjectResultaat){

$ TEMP = @()

foreach($ WmiObjectResultaat中的$ item){
如果($ ServiceAccounts-包含$ Item.StartName){
$ TEMP + = $ Item

$ info = @ {
'名称'= $ TEMP.Name;
'开始名称'= $ TEMP。开始名称
}
}
}

$ Srvobj =新对象-TypeName PSObject-属性$ info
$ obj.ServiceName + = $ Srvobj.Name
$ obj.StartName + = $ Srvobj.Startname

}

$ obj | Export-Csv-路径“D:\ Scripts \ Test-Peter \ Testing \ lalala.csv” -NoTypeInformation

最佳答案

您已经创建了一个对象$obj来保存所有任务详细信息,而不是单个任务的集合:

$obj = New-Object –TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName
$obj | Add-Member -MemberType NoteProperty -Name TaskName -Value ([string]::Join(",",(@())))
# ... and so on

同样,您无需将单个任务添加到数组/集合中,而是将每个属性的值添加到 $obj的相同属性中:
$tskobj = New-Object -TypeName PSObject -Property $info 
$obj.TaskName += $tskobj.TaskName
$obj.ScheduledTaskState += $tskobj.ScheduledTaskState
$obj.LogonMode += $tskobj.LogonMode
$obj.Author += $tskobj.Author
$obj.RunAsUser += $tskobj.RunAsUser

您需要做的就是将 $obj更改为一个空集合,然后将任务分配给它:
$obj = @()

foreach($Task in (Get-ScheduledTask)){
$TaskProperties = @{
'TaskName'=$Task.TaskName;
'ScheduledTaskState'=$Task.ScheduledTaskState;
'LogonMode'=$Task.LogonMode;
'Author'=$Task.Author;
'RunAsUser'=$Task.RunAsUser
}
$obj += New-Object psobject -Property $TaskProperties
}

$obj | Export-Csv "output.csv"

关于csv - Powershell Export-Csv提供了不良结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com