<#
.SYNOPSIS
    A short one-line action-based description, e.g. 'Tests if a function is valid'
.DESCRIPTION
    A longer description of the function, its purpose, common use cases, etc.
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    Specify a URI to a help page, this will show when Get-Help -Online is used.
.EXAMPLE
    Test-MyTestFunction -Verbose
    Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
#>



<# Scheduled Task metadata
General
    Get IPs for PA
    run as ndgov\!itdvcenterppa
        run whether user is logged on or not
 Triggers
    Daily, 11am
    Daily, 11pm
Actions
    old-C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\GetIPs.ps1"
    new-"C:\Program Files\PowerShell\7\pwsh.exe" -noninteractive -file "F:\GetVMwareVMGuestIPsForPA\GetVMwareVMGuestIPsForPA.ps1"

Settings
    allow task to be run on demand
    stop the task if it runs longer than 1 hour -eq $true
    if the running task does not end when requested, force it to stop
#>

$TimeStamp = Get-Date -UFormat "%Y%m%d%H%M%S"

#Connect
Connect-ITDvCenter -Credential $Secret:ndgov_svcitdvmvcauto


##Windows


#Output File
$OutFileWin = "c:\inetpub\wwwroot\Win.txt"
#$Date = Get-Date -UFormat "%Y%m%d%H%M%S"
Get-Item -Path $OutFileWin | Copy-Item -Destination "F:\GetVMwareVMGuestIPsForPA\Backup\Win\$Timestamp-Win.txt"
Remove-Item $OutFileWin
Start-Sleep -Seconds 5

#Get Powered On VM's
$vmwin = get-VM | Where-Object { $_.PowerState -eq "PoweredOn" `
        -and ($_.GuestID -eq "windows7Guest" `
            -or $_.GuestID -eq "windows7_64Guest" `
            -or $_.GuestID -eq "windows7Server64Guest" `
            -or $_.GuestID -eq "windows8_64Guest" `
            -or $_.GuestID -eq "windows8Server64Guest" `
            -or $_.GuestID -eq "windows9Server64Guest" `
            -or $_.GuestID -eq "winLonghorn64Guest" `
            -or $_.GuestID -eq "winLonghornGuest" `
            -or $_.GuestID -eq "winNetStandardGuest" `
            -or $_.GuestID -eq "winNetEnterpriseGuest" `
            -or $_.GuestID -eq "windows9_64Guest" `
            -or $_.GuestID -eq "windows2019srv_64Guest" `
            -or $_.GuestID -eq "windows2019srvNext_64Guest") }
$vmviewwin = $vmwin | Get-View

$Outputwin = ""

#Loop through VM's, NIC's, and IP addresses.
Foreach ($v in $vmviewwin) {
    Foreach ($nic in $v.Guest.Net) {
        Foreach ($IP in $nic.IPAddress) {
            If ($IP -notlike "fe80*" -and $IP -notlike "192.168.*" -and $IP -notlike "172.16*") {
                    $OutputWin += $IP + "`n"
            }
        }
    }
}

#If ($Outputwin -ne "") {$OutputWin | Out-File $OutFileWin -Encoding utf8 -NoNewline}
If ($Outputwin -ne "") { $OutputWin | Out-File $OutFileWin -Encoding ASCII -NoNewline }



##Linux

#Output File
$OutFileLin = "c:\inetpub\wwwroot\Lin.txt"
$Date = Get-Date -UFormat "%Y%m%d%H%M%S"
Get-Item -Path $OutFileLin | Copy-Item -Destination "F:\GetVMwareVMGuestIPsForPA\Backup\Lin\$Timestamp-Lin.txt"
Remove-Item $OutFileLin
Start-Sleep -Seconds 5

#Get Powered On VM's
$vmLin = get-VM | Where-Object { $_.PowerState -eq "PoweredOn" `
        -and ($_.GuestID -eq "centos6_64Guest" `
            -or $_.GuestID -eq "centos64Guest" `
            -or $_.GuestID -eq "centos7_64Guest" `
            -or $_.GuestID -eq "oracleLinux64Guest" `
            -or $_.GuestID -eq "oracleLinux7_64Guest" `
            -or $_.GuestID -eq "rhel4Guest" `
            -or $_.GuestID -eq "rhel5Guest" `
            -or $_.GuestID -eq "rhel5_64Guest" `
            -or $_.GuestID -eq "rhel6Guest" `
            -or $_.GuestID -eq "rhel6_64Guest" `
            -or $_.GuestID -eq "rhel7_64Guest" `
            -or $_.GuestID -eq "rhel8_64Guest" `
            -or $_.GuestID -eq "rhel9_64Guest" `
            -or $_.GuestID -eq "sles11_64Guest" `
            -or $_.GuestID -eq "sles12_64Guest" `
            -or $_.GuestID -eq "ubuntu64Guest") }
$vmviewlin = $vmLin | Get-View

$OutputLin = ""

#Loop through VM's, NIC's, and IP addresses.
Foreach ($v in $vmviewlin) {
    Foreach ($nic in $v.Guest.Net) {
        Foreach ($IP in $nic.IPAddress) {
            If ($IP -notlike "fe80*" -and $IP -notlike "192.168.*" -and $IP -notlike "172.16*") {
                    $OutputLin += $IP + "`n"
            }
        }
    }
}

#If ($OutputLin -ne "") {$OutputLin | Out-File $OutFileLin -Encoding utf8 -NoNewline}
If ($OutputLin -ne "") { $OutputLin | Out-File $OutFileLin -Encoding ASCII -NoNewline }

Disconnect-ITDvCenter