Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName PresentationFramework, WindowsBase, System.Windows.Forms
- $xaml=@"
- <Window Name='Window' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' WindowStartupLocation='CenterScreen' WindowStyle='None' WindowState='Normal' SizeToContent='WidthAndHeight' Background='#FFFFFF' ResizeMode='NoResize' AllowsTransparency='True'>
- <Border Name='bdr_Border' BorderThickness='2' VerticalAlignment='Top'>
- <Grid>
- <StackPanel>
- <DockPanel Name='dck_TitleBar' DockPanel.Dock='Top'>
- <Button Name='btn_Close' Content='X' DockPanel.Dock='Right' Width='Auto' Padding='15,0' FontSize='18' Foreground='White' Margin='5' Background='#EB3C01'/>
- <Button Name='btn_Minimize' Content='_' DockPanel.Dock='Right' Width='Auto' Padding='15,0' FontSize='18' Foreground='White' Margin='5,5,0,5' Background='Gray'/>
- <Label Name = 'lbl_AppTitle' HorizontalAlignment='Left' VerticalAlignment='Center' FontSize='18' Foreground='White' DockPanel.Dock='Left' Padding='10'/>
- </DockPanel>
- <DockPanel LastChildFill='False' Margin='10'>
- <Border DockPanel.Dock='Top' BorderThickness='1' BorderBrush='#CCCCCC'>
- <DockPanel Margin='10'>
- <StackPanel>
- <DockPanel>
- <StackPanel Margin='0,0,10,0'>
- <Label Content='Site Server' FontSize='14'/>
- <TextBox Name='txt_SiteServer' Width='350' FontSize='14' Height='25'/>
- </StackPanel>
- <StackPanel Margin='0,0,10,0'>
- <Label Content='Site Code' FontSize='14'/>
- <TextBox Name='txt_SiteCode' Width='70' FontSize='14' Height='25'/>
- </StackPanel>
- </DockPanel>
- <StackPanel Margin='0,0,10,0'>
- <Label Content='Package Exclusions (Comma separated)' FontSize='14'/>
- <TextBox Name='txt_PkgExclusions' Width='430' FontSize='14' Height='25'/>
- </StackPanel>
- </StackPanel>
- <StackPanel Height='110'>
- <Button Name='btn_Load' Content='Load' Width='50' Height='20' FontSize='12' Margin='0,29,0,0'/>
- <Button Name='btn_Save' Content='Save' Width='50' Height='20' FontSize='12' Margin='0,8,0,0'/>
- <Button Name='btn_Refresh' Content='Refresh' Width='50' Height='20' FontSize='12' Margin='0,8,0,0'/>
- </StackPanel>
- </DockPanel>
- </Border>
- </DockPanel>
- <DockPanel Margin='20,0,20,0'>
- <StackPanel Margin='0,0,10,0'>
- <Label Content='Distribution Point' FontSize='14'/>
- <ComboBox Name='cmb_DistributionPoint' Width='360' FontSize='14' Height='25'/>
- </StackPanel>
- <Button Name='btn_Check' Content='Check' VerticalAlignment='Bottom' Width='50' FontSize='12' Height='25' Margin='0,0,10,0' IsEnabled='False'/>
- <Button Name='btn_Validate' Content='Validate' VerticalAlignment='Bottom' Width='60' FontSize='12' Height='25' IsEnabled='False'/>
- </DockPanel>
- <DockPanel Margin='20,0,20,0' LastChildFill='False'>
- <Label Content='DP Status:' FontSize='12' Height='30' VerticalContentAlignment='Center'/>
- <Label Name='lbl_DPStatus' Content='---' FontSize='12' Width='80' Height='30' VerticalContentAlignment='Center'/>
- <MediaElement Name='med_DPStatus' UnloadedBehavior='Manual' Source='$($PSScriptRoot)\loading.GIF' LoadedBehavior='Play' Stretch='Uniform' Visibility='Collapsed' Width='80' Height='30' HorizontalAlignment='Left'/>
- <Label Content='Deployed Packages:' FontSize='12' Height='30' VerticalContentAlignment='Center'/>
- <Label Name='lbl_Deployed' Content='---' FontSize='12' Width='30' Height='30' VerticalContentAlignment='Center'/>
- <MediaElement Name='med_Deployed' UnloadedBehavior='Manual' Source='$($PSScriptRoot)\loading.GIF' LoadedBehavior='Play' Stretch='Uniform' Visibility='Collapsed' Width='30' Height='30'/>
- </DockPanel>
- <DockPanel Margin='20,0,20,0' LastChildFill='False'>
- <Label Content='Packages in WMI:' FontSize='12' Height='30'/>
- <Label Name='lbl_WMI' Content='---' FontSize='12' Width='40' Height='30'/>
- <MediaElement Name='med_WMI' UnloadedBehavior='Manual' Source='$($PSScriptRoot)\loading.GIF' LoadedBehavior='Play' Stretch='Uniform' Visibility='Collapsed' Width='40' Height='30'/>
- <Label Content='Packages in PkgLib: ' FontSize='12' Height='30'/>
- <Label Name='lbl_PkgLib' Content='---' FontSize='12' Width='30' Height='30'/>
- <MediaElement Name='med_PkgLib' UnloadedBehavior='Manual' Source='$($PSScriptRoot)\loading.GIF' LoadedBehavior='Play' Stretch='Uniform' Visibility='Collapsed' Width='30' Height='30'/>
- </DockPanel>
- <DockPanel Margin='20,10,20,20' LastChildFill='False' HorizontalAlignment='Center'>
- <StackPanel Margin='0,0,20,0'>
- <Label Content='Invalid WMI Entries' FontSize='14' HorizontalAlignment='Center'/>
- <ListBox Name='lst_InvalidWMI' FontSize='14' Height='200' Width='150' SelectionMode='Multiple'/>
- <Button Name='btn_InvalidWMI' Content='Remove Selected' FontSize='12' Margin='0,10,0,0' Height='25' IsEnabled='False'/>
- </StackPanel>
- <StackPanel Margin='0,0,20,0'>
- <Label Content='Invalid PkgLib Entries' FontSize='14' HorizontalAlignment='Center'/>
- <ListBox Name='lst_InvalidPkgLib' FontSize='14' Height='200' Width='150'/>
- <Button Name='btn_InvalidPkgLib' Content='Remove Selected' FontSize='12' Margin='0,10,0,0' Height='25' IsEnabled='False'/>
- </StackPanel>
- <StackPanel>
- <Label Content='Missing Content' FontSize='14' HorizontalAlignment='Center'/>
- <ListBox Name='lst_MissingContent' FontSize='14' Height='200' Width='150' SelectionMode='Multiple'/>
- <Button Name='btn_MissingContent' Content='Redistribute Selected' FontSize='12' Margin='0,10,0,0' Height='25' IsEnabled='False'/>
- </StackPanel>
- </DockPanel>
- </StackPanel>
- </Grid>
- </Border>
- </Window>
- "@
- $xaml=[xml]$xaml
- $Form = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml))
- $Controls = @{}
- $xaml.SelectNodes("//*[@Name]") | ForEach-Object {$Controls.Add($_.Name,$Form.FindName($_.Name))}
- $Controls.lbl_AppTitle.Content="Distribution Point Content Validation Tool"
- $Controls.Window.Title="Distribution Point Content Validation Tool"
- $Controls.dck_TitleBar.Add_MouseDown{$Controls.Window.DragMove()}
- $Controls.btn_Minimize.Add_Click{$Form.WindowState='Minimized'}
- $Controls.btn_Close.Add_Click{$Form.close()}
- $Controls.btn_Save.Add_Click{Save-Config}
- $Controls.btn_Check.Add_Click{Check-DistributionPoint}
- $Controls.btn_InvalidWMI.Add_Click{Remove-InvalidWMI}
- $Controls.btn_InvalidPkgLib.Add_Click{Remove-InvalidPkgLib}
- $Controls.btn_Validate.Add_Click{Start-Validation}
- $Controls.cmb_DistributionPoint.Add_SelectionChanged{Reset-Form;Validate-Buttons}
- $Controls.btn_Load.Add_Click{
- Get-Config
- Connect-ConfigMgr
- Get-DistributionPoints
- }
- $Controls.btn_Refresh.Add_Click{
- Connect-ConfigMgr
- Get-DistributionPoints
- }
- $Controls.btn_MissingContent.Add_Click{
- Redist-MissingContent
- }
- Foreach ($Control in $Controls.med_DPStatus,$Controls.med_Deployed,$Controls.med_WMI,$Controls.med_PkgLib){
- $Control.Add_MediaEnded{
- If ($args[0].Visibility -eq "Visible"){
- $args[0].Position=New-TimeSpan -Seconds 1
- $args[0].Play()
- }
- }
- }
- $Script:Running=$False
- $Controls.Window.Add_Loaded{
- Get-Config
- Connect-ConfigMgr
- Get-DistributionPoints
- Get-FailedContent
- }
- $ThemeColour=[System.Windows.SystemParameters]::WindowGlassBrush
- Foreach ($Control in $Controls.btn_Save,$Controls.btn_Load,$Controls.btn_Refresh,$Controls.btn_Check,$Controls.btn_InvalidWMI,$Controls.btn_MissingContent,$Controls.btn_InvalidPkgLib,$Controls.btn_Validate){
- $Control.Background=$ThemeColour
- $Control.Foreground='White'
- }
- $Controls.dck_TitleBar.Background=$ThemeColour
- $Controls.bdr_Border.BorderBrush=$ThemeColour
- Function Save-Config
- {
- Try{
- $Controls.txt_SiteServer.Text | Out-File "$($PSScriptRoot)\CMSite.dat" -Force -ErrorAction Stop
- $Controls.txt_SiteCode.Text | Out-File "$($PSScriptRoot)\CMSite.dat" -Append -ErrorAction Stop
- $Controls.txt_PkgExclusions.Text | Out-File "$($PSScriptRoot)\CMSite.dat" -Append -ErrorAction Stop
- [System.Windows.MessageBox]::Show('Configuration Saved','Success','OK','Information')
- }
- Catch{
- [System.Windows.MessageBox]::Show('Error Saving Configuration','Error','OK','Error')
- }
- }
- Function Get-Config
- {
- If (Test-Path "$($PSScriptRoot)\CMSite.dat"){
- $Config=Get-Content "$($PSScriptRoot)\CMSite.dat"
- $Controls.txt_SiteServer.Text=$Config[0]
- $Controls.txt_Sitecode.Text=$Config[1]
- $Controls.txt_PkgExclusions.Text=$Config[2]
- }
- }
- Function Connect-ConfigMgr
- {
- $SiteCode = $Controls.txt_SiteCode.Text
- $ProviderMachineName = $Controls.txt_SiteServer.Text
- if($null -eq (Get-Module ConfigurationManager)) {
- Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
- }
- if($null -eq (Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue)) {
- New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName
- }
- }
- Function Get-DistributionPoints
- {
- $Controls.cmb_DistributionPoint.Items.Clear()
- Set-Location "$($Controls.txt_SiteCode.Text):"
- Get-CMDistributionPointInfo | where {$_.OperatingSystem -notlike "*Azure*"} | Foreach-Object {$Controls.cmb_DistributionPoint.Items.Add($_.Name)}
- }
- Function Get-FailedContent
- {
- $SiteCode = $Controls.txt_SiteCode.Text
- $Script:failedContent = Get-WmiObject -Namespace root\sms\site_$SiteCode -Query "SELECT * FROM SMS_PackageStatusDistPointsSummarizer WHERE State > 1" | Select-Object ServerNALPath,PackageID
- }
- Function Check-DistributionPoint
- {
- $Script:Running=$True
- $Controls.btn_Check.IsEnabled=$False
- Reset-Form
- Get-DPStatus
- Get-DeployedPackages
- Get-WMIPackages
- Get-PkgLibPackages
- Populate-MissingContent
- $Script:Running=$False
- Validate-Buttons
- }
- Function Get-DPStatus
- {
- $Controls.lbl_DPStatus.Visibility="Collapsed"
- $Controls.med_DPStatus.Visibility="Visible"
- $Controls.med_DPStatus.Position=New-TimeSpan -Seconds 1
- $Controls.med_DPStatus.Play()
- [System.Windows.Forms.Application]::DoEvents()
- if ( -not(Test-Connection $Controls.cmb_DistributionPoint.Text -Quiet -Count 1)) {
- $Controls.lbl_DPStatus.Content="Offline"
- $Controls.lbl_DPStatus.Foreground="Red"
- Break
- } else {
- Set-Location "$($Controls.txt_SiteCode.Text):"
- $Script:ContentLibDrive=(Get-CMDistributionPoint $Controls.cmb_DistributionPoint.Text | Get-CMDistributionPointDriveInfo).Drive
- If ($Script:ContentLibDrive.Count -ne 1){
- $Script:ContentLibDrive=Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ArgumentList $Script:ContentLibDrive -ScriptBlock {
- If ($Args.count -gt 0){
- Foreach ($Item in $Args){
- If (Test-Path -Path "$($Item):\SCCMContentLib\PkgLib"){
- return $Item
- break
- }
- }
- } else {
- $Drives = (Get-Volume | Where-Object {$_.DriveType -eq "Fixed" -and $_.DriveLetter}).DriveLetter
- foreach ($Item in $Drives){
- If (Test-Path -Path "$($Item):\SCCMContentLib\PkgLib"){
- return $Item
- break
- }
- }
- }
- }
- }
- $Controls.lbl_DPStatus.Content="Online"
- $Controls.lbl_DPStatus.Foreground="Green"
- }
- $Controls.lbl_DPStatus.Visibility="Visible"
- $Controls.med_DPStatus.Visibility="Collapsed"
- [System.Windows.Forms.Application]::DoEvents()
- }
- Function Get-PkgLibPackages
- {
- $Controls.lbl_PkgLib.Visibility="Collapsed"
- $Controls.med_PkgLib.Visibility="Visible"
- $Controls.med_PkgLib.Position=New-TimeSpan -Seconds 1
- $Controls.med_PkgLib.Play()
- [System.Windows.Forms.Application]::DoEvents()
- Set-Location C:
- $Script:PkgLibPackageList = @(Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ArgumentList $Script:ContentLibDrive -ScriptBlock {
- Get-ChildItem "$($args[0]):\SCCMContentLib\PkgLib" | Select -ExpandProperty Name
- })
- $Script:PkgLibPackageList = $Script:PkgLibPackageList | ForEach-Object {$_.replace('.INI',"")}
- $Script:PkgLibPackageList = $Script:PkgLibPackageList | Where-Object {$_ -notin $Exclusions}
- $Controls.lbl_PkgLib.Content="$($Script:PkgLibPackageList.Count)"
- If ($Controls.lbl_PkgLib.Content -ne $Controls.lbl_Deployed.Content){
- $Controls.lbl_PkgLib.Foreground='Red'
- } else {
- $Controls.lbl_PkgLib.Foreground='Green'
- }
- $Controls.lbl_PkgLib.Visibility="Visible"
- $Controls.med_PkgLib.Visibility="Collapsed"
- [System.Windows.Forms.Application]::DoEvents()
- Populate-InvalidPkgLib
- }
- Function Get-DeployedPackages
- {
- $Controls.lbl_Deployed.Visibility="Collapsed"
- $Controls.med_Deployed.Visibility="Visible"
- $Controls.med_Deployed.Position=New-TimeSpan -Seconds 1
- $Controls.med_Deployed.Play()
- [System.Windows.Forms.Application]::DoEvents()
- Set-Location "$($Controls.txt_SiteCode.Text):"
- $Script:Exclusions=$Controls.txt_PkgExclusions.Text.Split(",")
- $Script:DPPackageList=Get-CMDeploymentPackage -DistributionPointName $Controls.cmb_DistributionPoint.Text | where {$_.PackageID -notin $Exclusions} | Select-Object -ExpandProperty PackageID
- $Controls.lbl_Deployed.Content= "$($Script:DPPackageList.count)"
- $Controls.lbl_Deployed.Visibility="Visible"
- $Controls.med_Deployed.Visibility="Collapsed"
- [System.Windows.Forms.Application]::DoEvents()
- }
- Function Get-WMIPackages
- {
- $Controls.lbl_WMI.Visibility="Collapsed"
- $Controls.med_WMI.Visibility="Visible"
- $Controls.med_WMI.Position=New-TimeSpan -Seconds 1
- $Controls.med_WMI.Play()
- [System.Windows.Forms.Application]::DoEvents()
- Set-Location "C:\"
- $Script:WMIPackageList = Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ScriptBlock {@(Get-WMIObject -Namespace "root\sccmdp" -Query "Select * from SMS_PackagesInContLib " | Select-Object -ExpandProperty PackageID)}
- $Script:WMIPackageList = $Script:WMIPackageList | Where-Object {$_ -notin $Exclusions}
- $Controls.lbl_WMI.Content="$($Script:WMIPackageList.Count)"
- If ($Controls.lbl_WMI.Content -ne $Controls.lbl_Deployed.Content){
- $Controls.lbl_WMI.Foreground='Red'
- } else {
- $Controls.lbl_WMI.Foreground='Green'
- }
- $Controls.lbl_WMI.Visibility="Visible"
- $Controls.med_WMI.Visibility="Collapsed"
- [System.Windows.Forms.Application]::DoEvents()
- Populate-InvalidWMI
- }
- Function Populate-InvalidWMI
- {
- $Controls.lst_InvalidWMI.Items.Clear()
- Foreach ($WMIPackage in $Script:WMIPackageList){
- if ($Script:DPPackageList -notcontains $WMIPackage){
- $Controls.lst_InvalidWMI.Items.Add($WMIPackage)
- }
- }
- [System.Windows.Forms.Application]::DoEvents()
- }
- Function Remove-InvalidWMI{
- Invoke-command -ComputerName $Controls.cmb_DistributionPoint.Text -ArgumentList $Controls.lst_InvalidWMI.SelectedItems -ScriptBlock {
- Foreach ($Item in $Args){
- Get-WMIObject -Namespace "root\sccmdp" -Query ("Select * from SMS_PackagesInContLib where PackageID = '" + ([string]($Item)) + "'") | Remove-WmiObject
- }
- }
- Get-WMIPackages
- Validate-Buttons
- }
- Function Populate-InvalidPkgLib
- {
- $Controls.lst_InvalidPkgLib.Items.Clear()
- Foreach ($PkgLibPackage in $Script:PkgLibPackageList){
- if ($Script:DPPackageList -notcontains $PkgLibPackage){
- $Controls.lst_InvalidPkgLib.Items.Add($PkgLibPackage)
- }
- }
- [System.Windows.Forms.Application]::DoEvents()
- }
- Function Remove-InvalidPkgLib
- {
- Set-Location C:
- Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ArgumentList $Script:ContentLibDrive,$Controls.lst_InvalidPkgLib.SelectedItems -ScriptBlock {
- Foreach ($Item in $Args[1..$Args.Length]){
- If (Test-Path "$($Args[0]):\SCCMContentLib\PkgLib\$($Item).INI"){
- Remove-Item "$($Args[0]):\SCCMContentLib\PkgLib\$($Item).INI"
- }
- }
- }
- Get-PkgLibPackages
- Validate-Buttons
- }
- Function Populate-MissingContent
- {
- $Controls.lst_MissingContent.Items.Clear()
- Foreach ($DPPackage in $Script:DPPackageList){
- if ($Script:WMIPackageList -notcontains $DPPackage -and $ScriptLPkgLibPackageList -notcontains $DPPackage){
- $Controls.lst_MissingContent.Items.Add($DPPackage)
- }
- }
- Foreach ($Package in $Script:FailedContent){
- if ( $Package.ServerNalPath -like "*$($Controls.cmb_DistributionPoint.Text)*"){
- $Controls.lst_MissingContent.Items.Add($Package.PackageID)
- }
- }
- }
- Function Redist-MissingContent
- {
- $SiteCode = $Controls.txt_SiteCode.Text
- Foreach ($Item in $Controls.lst_MissingContent.SelectedItems){
- $RedistPackage = Get-WmiObject -Namespace "root\SMS\Site_$Sitecode" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$Item' AND ServerNALPath like '%$($Controls.cmb_DistributionPoint.Text)%'"
- $RedistPackage.RefreshNow = $True
- $RedistPackage.Put() |out-null
- }
- Get-WMIPackages
- Get-PkgLibPackages
- Validate-Buttons
- }
- function Start-Validation
- {
- $Response=[System.Windows.MessageBox]::Show("Start the Content Validation Scheduled task on $($Controls.cmb_DistributionPoint.Text)?",'Content Validation','YesNo','Question')
- If ($Response="Yes"){
- $smsdpmonRunning=Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ScriptBlock {If ((Get-Process | Where-Object {$_.Name -eq "smsdpmon"}).Count -gt 0){$True}else{$False}}
- If ($smsdpmonRunning -ne $True){
- Try{
- Invoke-Command -ComputerName $Controls.cmb_DistributionPoint.Text -ScriptBlock {Get-ScheduledTask -TaskName "Content Validation" | Start-ScheduledTask} -ErrorAction Stop
- [System.Windows.MessageBox]::Show('Content Validation started','Success','OK','Information')
- }
- Catch{
- [System.Windows.MessageBox]::Show('Error starting Content Validation','Error','OK','Error')
- }
- } else {
- [System.Windows.MessageBox]::Show("Content Validation is already running on $($Controls.cmb_DistributionPoint.Text).",'Error','OK','Error')
- }
- }
- }
- function Validate-Buttons
- {
- If ($Controls.cmb_DistributionPoint.SelectedIndex -ge 0 -and $Script:Running -eq $False){
- $Controls.btn_Check.IsEnabled=$True
- } else {
- $Controls.btn_Check.IsEnabled=$False
- }
- If (($Controls.lbl_DPStatus.Content -eq "Online") -and ($Controls.lbl_PkgLib.Content-eq $Controls.lbl_Deployed.Content) -and ($Controls.lbl_PkgLib.Content-eq $Controls.lbl_Deployed.Content)){
- $Controls.btn_Validate.IsEnabled=$True
- } else {
- $Controls.btn_Validate.IsEnabled=$False
- }
- If ($Controls.lst_InvalidWMI.Items.Count -gt 0){$Controls.btn_InvalidWMI.IsEnabled = $True} else {$Controls.btn_InvalidWMI.IsEnabled = $False}
- If ($Controls.lst_InvalidPkgLib.Items.Count -gt 0){$Controls.btn_InvalidPkgLib.IsEnabled = $True} else {$Controls.btn_InvalidPkgLib.IsEnabled = $False}
- If ($Controls.lst_MissingContent.Items.Count -gt 0){$Controls.btn_MissingContent.IsEnabled = $True} else {$Controls.btn_MissingContent.IsEnabled = $False}
- }
- Function Reset-Form
- {
- Foreach ($Control in $Controls.lbl_DPStatus,$Controls.lbl_Deployed,$Controls.lbl_WMI,$Controls.lbl_PkgLib){
- $Control.Content="---"
- $Control.Visibility="Visible"
- $Control.Foreground="Black"
- }
- Foreach ($Control in $Controls.med_DPStatus,$Controls.med_Deployed){
- $Control.Visibility="Collapsed"
- }
- $Controls.lst_InvalidWMI.Items.Clear()
- $Controls.lst_InvalidPkgLib.Items.Clear()
- $Controls.lst_MissingContent.Items.Clear()
- [System.Windows.Forms.Application]::DoEvents()
- }
- [void]$Form.Showdialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement