Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to add an exclude to this script I'm using

$
0
0

Hello,

I'm using the following script to parse out data from an input file.  It's a list of computers that will be moved in our Hyper-V environment to their respective hosts.

I want to add an exclude statement to not add in certain computernames to the output file if they exist in the input file.  Here is my script:

#********************************************************************************************************
# Split Function to read Move_script.txt file and create files of 25 records by Host name.
#********************************************************************************************************

#Begin
Set-Location '.\'
$Cluster1 = "\\Cluster1\d$\ClusterGroup\Move_script.txt"
$Cluster2 = "\\Cluster2\d$\ClusterGroup\Move_script.txt"


#Select the Host you want to create files for.  The user input is not case sensitive:
$HostName = Read-Host 'Please provide the full name of the Hyper-v Host'


#If/ElseIf/Else method to determine the location of the Move_ClusterGroup_script.txt file:
If ($HostName -eq 'Host1') { $clusterFile = $Cluster1 }
ElseIf ($HostName -eq 'Host2') { $clusterFile = $Cluster1}
ElseIf ($HostName -eq 'Host3') { $clusterFile = $Cluster1}
ElseIf ($HostName -eq 'Host4') { $clusterFile = $Cluster2}
ElseIf ($HostName -eq 'Host5') { $clusterFile = $Cluster2}
Else { Write-Host 'Host name not found' }


#Read through the clusterFile and split the results into files of 25 each.  Remainder into the last file.
Function Split-File{

    Process {
        Remove-Item .\$($_)* -Force #remove existing files
        $currentfile=Get-Content "$clusterFile" | Select-String -Pattern $_
        $filecounter=1
        For($i=0;$i -le @($currentfile).Count;$i++){
            $currentfile[$i] | Add-Content ".\$($_)_$($filecounter).txt"
            If(!(($i+1)%25)){$filecounter++}
            }
    }
}

$HostName | Split-File
#Get back to work
Thank you.

Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>