【问题标题】:Identify given computer domain if they have Skype for Business Application识别给定的计算机域(如果他们有 Skype for Business 应用程序)
【发布时间】:2019-09-11 18:12:10
【问题描述】:

我需要您对我的 PowerShell 脚本的帮助和建议,已经有可以在终端屏幕上显示它的初始程序,我的问题是我想将终端屏幕的输出导出到 CSV 文件

computername.txt里面:

d-pol-abanaga d-pol-pcspc d-极点

输出终端画面:

PS C:\Users\ksaycon\Desktop> ./script.ps1 d-pol-abanaga 没有 Skype for Business d-pol-pcspc 不存在 Skype for Business d-pol-eplete 没有 Skype for Business

这应该是基于终端输出的导出 CSV:

d-pol-abanaga  No Skype for Business are present
d-pol-pcspc  No Skype for Business are present
d-pol-eplete  No Skype for Business are present

代码:

# Read input file of computer names
$computers = Get-Content C:\pstool\computername.txt
# Loop through each computer name in the array
foreach ($computer in $computers) {
    # Test for lync.exe file
    $path = Test-Path "<\\$computer\c$\program files\microsoft office\office15\*>" -Include lync.exe
    # Report if templates are found or not
    if ($path -eq $true) {
        Write-Output $Computer 'Skype for Business are present' |
            Export-Csv -Path "output.csv" -NoTypeInformation -Append
    } else {
        if ($path -eq $false) {
            $path16 = Test-Path "<\\$computer\c$\program files\microsoft office\office16\*>" -Include lync.exe
        }
    }

    if ($path16 -eq $true) {
        Write-Output $Computer 'Skype for Business are present' |
            Export-Csv -Path "output.csv" -NoTypeInformation -Append
    } else {
        Write-Output $Computer ' No Skype for Business are present' |
            Export-Csv -Path "output.csv" -NoTypeInformation -Append
    }
}

【问题讨论】:

  • 从路径中删除尖括号。此外,运行脚本的用户是否在远程计算机上具有管理员权限?否则他们无权访问管理共享 (\\$computer\C$)。
  • 谢谢,但我的问题是当导出到 CSV 时它有空数据,没有复制 csv 文件中的确切输出。 :(
  • 那么请edit您的问题并显示minimal reproducible example。当您的问题无法让我们理解或重现您所面临的问题时,我们无法为您提供帮助。
  • @AnsgarWiechers 已经编辑,如果我的问题有太多华丽的东西,我很抱歉。我不得不多次表达。请理解我。谢谢
  • 取决于您要导出的内容。您可以在循环中使用Export-Csv -Append,但您不能将Write-Host 输出导出到文件,因为该输出会直接发送到主机控制台。如果您希望通过管道或重定向处理输出,请使用 Write-Output 而不是 Write-Host

标签: windows powershell scripting


【解决方案1】:

我认为循环访问您感兴趣的 Office 版本并在其中一个中找到 Skype 时立即中断循环会容易得多。接下来使用您需要的信息创建一个对象,并将其收集到变量 $result 中。

类似这样的:

# Loop through each computer name in the array from file 'C:\pstool\computername.txt'
# collect the data for export in variable $result
$result = foreach ($computer in (Get-Content 'C:\pstool\computername.txt')) {
    # Test for lync.exe file in Office versions 15 and 16
    $skype = 'Not present'
    for ($v = 15; $v -lt 17; $v++) {
        if (Test-Path -Path "\\$computer\c$\program files\microsoft office\office$v\*" -Include lync.exe) {
            $skype = 'Present'
            break
        }
    }

    # emit a PSObject to collect in the $result variable
    [PsCustomObject]@{
        'Computer'           = $computer
        'Skype for Business' = $skype
    }
}

# output to console
$result

# write results to CSV
$result | Export-Csv -Path "D:\output.csv" -NoTypeInformation -Append

【讨论】:

    【解决方案2】:

    在您的问题中看不到您如何尝试导出 CSV,这将是最有趣的部分。

    从这里复制导出部分:https://stackoverflow.com/a/21047532/12029319

    # Read input file of computer names
    $computers = Get-Content C:\pstool\computername.txt
    $Application = "lync.exe"
    $results = @()
    # Loop through each computer name in the array
    foreach ($computer in $computers) {
        # Test for $Application file
        $path = Test-Path "\\$computer\c$\program files\microsoft office\office15\*" -Include $Application
        # Report if templates are found or not
        if ($path -eq $true ) {
            Write-Host $Computer 'Skype for Business are present'
        }
        else {
            if ($path -eq $false) {
                $path16 = Test-Path "\\$computer\c$\program files\microsoft office\office16\*" -Include $Application
            }
        }
    
        if ($path16 -eq $true) {
            $result = 'Skype for Business are present'
            Write-Host $Computer $result
            $details = @{      
                ComputerName = $Computer
                Result       = $result    
            }
        }
        else {
            $result = 'No Skype for Business are present'
            Write-Host $Computer $result
            $details = @{      
                ComputerName = $Computer
                Result       = $result
            }
        }
        $results += New-Object PSObject -Property $details  
    }
    $results | export-csv -Path c:\pstool\Check_for_lync.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多