简体   繁体   中英

PowerShell: Test-Connection

I have two scripts which are running perfect, if I start them alone. Script 1:

    $serverName = "server01"
    Do
    {
    Test-Connection -ComputerName $serverName -ErrorAction SilentlyContinue -ErrorVariable pingError | 
    Out-Null
    $pingError = $pingError | Where-Object { $_.CategoryInfo.Category -Eq 'ResourceUnavailable' }
    Start-Sleep -Seconds 5
      }
    While($pingError -Ne $Null)

Script 2

      [cmdletbinding()]
    param(
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = $env:computername,
    [string]$OutputFile = "\\server01\temp\ergebnis.csv"
    ) 
    $Model = Get-WmiObject Win32_ComputerSystem | Select -Expand Model
    function Get-InstalledApps
    # This function will loop through the applications installed on one PC
    # and output one object for each Application with all its properties.
    # optionally saving/appending to a .CSV spreadsheet.
    {
        foreach ($App in $Applications)
        {           
            $AppRegistryKey = $UninstallRegKey + "\\" + $App
            $AppDetails = $HKLM.OpenSubKey($AppRegistryKey)
            #$AppGUID = $App
            if (($($AppDetails.GetValue("DisplayName")) -notlike "Security Update*") -and 
       ($($AppDetails.GetValue("DisplayName")) -notlike "Microsoft App Update for*") -and 
       ($($AppDetails.GetValue("DisplayName")) -notlike "Update for Microsoft*") )
            {
                $AppDisplayName = $($AppDetails.GetValue("DisplayName"))
                $AppVersion = $($AppDetails.GetValue("DisplayVersion"))
                #$AppPublisher = $($AppDetails.GetValue("Publisher"))
                #$AppInstalledDate = $($AppDetails.GetValue("InstallDate"))
                #$AppUninstall = $($AppDetails.GetValue("UninstallString"))
                if(!$AppDisplayName) { continue }
                $OutputObj = New-Object -TypeName PSobject
                #$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value 
     $Computer.ToUpper()
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppName -Value $AppDisplayName
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppVersion -Value $AppVersion
                #$OutputObj | Add-Member -MemberType NoteProperty -Name AppVendor -Value $AppPublisher
                #$OutputObj | Add-Member -MemberType NoteProperty -Name InstalledDate -Value $AppInstalledDate
                $OutputObj | Add-Member -MemberType NoteProperty -Name Typ -Value $Model
                #$OutputObj | Add-Member -MemberType NoteProperty -Name UninstallKey -Value $AppUninstall
                #$OutputObj | Add-Member -MemberType NoteProperty -Name AppGUID -Value $AppGUID
                #if ($RegistryView -eq 'Registry32')
                #{
                    #$OutputObj | Add-Member -MemberType NoteProperty -Name Arch -Value '32'
                #} else {
                #   $OutputObj | Add-Member -MemberType NoteProperty -Name Arch -Value '64'
                #}
                $OutputObj
            # Export to a file
            $OutputObj | export-csv -append -NoType -Encoding UTF8 -Delimiter ";" -path $OutputFile 
            }
        }
    }

    if((Test-Path "\\server01\temp") -eq $false){
    New-Item -Path "\\server01\temp" -ItemType Directory -Force}

    $UninstallRegKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
    Remove-Item $OutputFile -ErrorAction SilentlyContinue


    
    foreach($Computer in $ComputerName)
    {
        Write-Output "Computer: $Computer" 
        if(Test-Connection -ComputerName $Computer -Count 1 -ea 0)
        {
            # Get the architecture 32/64 bit
            if ((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computer -ea 0).OSArchitecture -eq '64-bit')
            {
                # If 64 bit check both 32 and 64 bit locations in the registry
                $RegistryViews = @('Registry32','Registry64')
            } else {
                # Otherwise only 32 bit
                $RegistryViews = @('Registry32')
            }

            foreach ( $RegistryView in $RegistryViews )
            {
                # Get the reg key(s) where add/remove program information is stored.
                $HKLM = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computer,$RegistryView)
                $UninstallRef = $HKLM.OpenSubKey($UninstallRegKey)
                $Applications = $UninstallRef.GetSubKeyNames()

                # Now we have the registry locations, call the function which will enumerate
                # all the applications on this PC
                Get-InstalledApps
            }
        }
    }

But if I now put the first script before the second, it aborts with an error message. However, I can't make out all of this because all parameters are given. The aim is that the inventory script is only executed when the server can be reached.

This is my failure:

In Zeile:11 Zeichen:1
+ [cmdletbinding()]
+ 
Unerwartetes Attribut "cmdletbinding".
In Zeile:12 Zeichen:1
+ param(

Unexpected token "param" in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedAttribute

The param directive must always come first in a script. If you combine the two scripts, move this part to the top of the file:

[CmdletBinding()]
param (
    [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
    [string[]]$ComputerName = $Env:ComputerName,
    [string]$OutputFile = "\\server01\temp\ergebnis.csv"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM