chkdsk
Goto Top

Powershell: Test-Connection

Moin Moin Zusammen,

ich habe folgende zwei Skripte, welche eigenständig auch ohne Fehler laufen.

Skript 1: Test-Connection
$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)

Skript 2: Software Inventur
##########################################################################################################################################################################################################
[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
            }
        }
    }

Setze ich jetzt aber das erste Skript vor das zweite, bricht er mit einer Fehlermeldung ab. Ich werde allerdings nicht ganz schlau daraus, da doch alle Parameter gegeben sind.

Ziel ist es, dass das Inventurskript nur dann ausgeführt wird, wenn der Server auch erreichbar ist.

Content-Key: 611561

Url: https://administrator.de/contentid/611561

Ausgedruckt am: 29.03.2024 um 10:03 Uhr

Mitglied: 143611
143611 09.10.2020 aktualisiert um 19:58:15 Uhr
Goto Top
Moin,

könntest Du die Fehlermeldung 1x posten? ^^

Viele Grüße,
schleeke
Mitglied: chkdsk
chkdsk 10.10.2020 aktualisiert um 14:33:02 Uhr
Goto Top
Sorry, mein Fehler face-smile
In Zeile:11 Zeichen:1
+ [cmdletbinding()]
+ ~~~~~~~~~~~~~~~~~
Unerwartetes Attribut "cmdletbinding".  
In Zeile:12 Zeichen:1
+ param(
+ ~~~~~
Unerwartetes Token "param" in Ausdruck oder Anweisung.  
    + CategoryInfo          : ParserError: (:) , ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedAttribute
Mitglied: chkdsk
chkdsk 10.10.2020 um 14:34:58 Uhr
Goto Top
Auch diese Frage hätte ich mir sparen können face-sad face-sad face-sad

hätte ich gesehen das ich nach dem kopieren des param an den Anfang eine Klammer vergessen habe... hätte hätte Fahrradkette. Trotzdem danke an alle