Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Please write a Powershell script that reads a text list of Server names, logins into to…

  1. User

    Please write a Powershell script that reads a text list of Server names, logins into to each Server in sequence with an account username and password, It then opens a cmd window and runs the command "java -XshowSettings:properties - version" for each Server on the list. It then creates an text file called "Java_Inventory.txt" that lists for each server the Server name, the Java Major version number, the Java Minor version number, the Java Build number, and the java vendor.

  2. Assistant
    # Define Input and Output filenames
    $inputFile = "ServerList.txt"
    $outputFile = "Java_Inventory.txt"
    
    # Read server names from file
    $servers = Get-Content $inputFile
    
    # Clear the old result file
    if (Test-Path $outputFile) {
        Remove-Item $outputFile
    }
    
    # Request user credentials once
    $credentials = Get-Credential
    
    foreach ($server in $servers) {
        # Connect to the Server using credentials
        $session = New-PSSession -ComputerName $server -Credential $credentials
    
        # Open a cmd window and run the specified command
        $javaCommandResult = Invoke-Command -Session $session -ScriptBlock {
            $output = cmd.exe /c "java -XshowSettings:properties -version 2>&1"
            $output -join "`n"
        }
    
        # Parse the result
        if ($javaCommandResult -match "version \"(\d+)\.(\d+)\.(\d+)([^"]*)\"") {
            $majorVersion = $matches[1]
            $minorVersion = $matches[2]
            $buildNumber = $matches[3]
        }
    
        if ($javaCommandResult -match "java.vendor = (.*)") {
            $javaVendor = $matches[1].Trim()
        }
    
        # Save the output to the text file
        @{
            Server          = $server
            MajorVersion    = $majorVersion
            MinorVersion    = $minorVersion
            BuildNumber     = $buildNumber
            JavaVendor      = $javaVendor
        } | Format-Table -AutoSize | Out-File $outputFile -Append
        
        # Close the session
        Remove-PSSession $session
    }
    
    #Display the result
    Get-Content $outputFile
    

    This PowerShell script reads a text file with a list of server names, logs into each server using the provided account username and password, then runs the specified Java command in a cmd window to get the required details, and saves the results to a text file called "Java_Inventory.txt".

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.