This commit is contained in:
Zack Meier
2026-04-15 15:45:50 -05:00
commit 1d304511b8
613 changed files with 140998 additions and 0 deletions
@@ -0,0 +1,49 @@
trigger:
- main
name: 'ITD.ITD-WindowsServer.FileManagement'
variables:
major: 0
minor: 7
patch: $(Build.BuildID)
buildVer: $(major).$(minor).$(Build.BuildID)
pool: itdwinautop1
stages:
- stage: Build
jobs:
- job: Build
steps:
- task: PowerShell@2
inputs:
filePath: '$(System.DefaultWorkingDirectory)/Build/build.ps1'
- task: NuGetCommand@2
inputs:
command: 'pack'
packagesToPack: '$(System.DefaultWorkingDirectory)/ITD.ITD-WindowsServer.FileManagement.nuspec'
versioningScheme: byEnvVar
versionEnvVar: buildVer
buildProperties: 'VERSIONHERE=$(buildVer)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'NuGetPackage'
publishLocation: 'Container'
- stage: Deploy
jobs:
- job: Deploy
steps:
- task: DownloadPipelineArtifact@2
inputs:
buildType: 'current'
artifactName: 'NuGetPackage'
itemPattern: '**'
targetPath: '$(Pipeline.Workspace)'
- task: NuGetCommand@2
inputs:
command: 'push'
packagesToPush: '$(Pipeline.Workspace)/ITD.ITD-WindowsServer.FileManagement.$(major).$(minor).$(Build.BuildID).nupkg'
nuGetFeedType: external
publishFeedCredentials: 'ITD_PwshGallery'
@@ -0,0 +1,17 @@
$buildVersion = $env:BUILDVER
$moduleName = 'ITD.ITD-WindowsServer.FileManagement'
$manifestPath = Join-Path -Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY -ChildPath "$moduleName.psd1"
$modulePath = Join-Path -Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY -ChildPath "$moduleName.psm1"
## Update build version in manifest
$manifestContent = Get-Content -Path $manifestPath -Raw
$manifestContent = $manifestContent -replace '<ModuleVersion>', $buildVersion
## Update functions to export in manifest
Import-Module $modulePath
$funcStrings = (Get-Module ITD.ITD-WindowsServer.FileManagement).ExportedCommands.Values.Name
$funcStrings = "'$($funcStrings -join "','")'"
$manifestContent = $manifestContent -replace "<FunctionsToExport>", $funcStrings
$manifestContent | Set-Content -Path $manifestPath
@@ -0,0 +1,21 @@
{
"ComputerName": "itdxyz.nd.gov",
"NotifyEmail": [
"emailA@nd.gov",
"emailB@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs\\LogFiles",
"Extension": "log",
"DaysToKeep": 90,
"Recursive": true
},
{
"Path": "C:\\temp",
"Extension": "txt",
"DaysToKeep": 30,
"Recursive": false
}
]
}
@@ -0,0 +1,26 @@
{
"ComputerName": "itddohslimst2.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs",
"Extension": "log",
"DaysToKeep": 120,
"Recursive": true
},
{
"Path": "C:\\Program Files\\STARLIMS\\STARLIMStst\\Log",
"Extension": "log",
"DaysToKeep": 120,
"Recursive": true
},
{
"Path": "E:\\Program Files\\STARLIMS\\STARLIMSdev\\Log",
"Extension": "log",
"DaysToKeep": 120,
"Recursive": true
}
]
}
@@ -0,0 +1,14 @@
{
"ComputerName": "itdernappt01.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs",
"Extension": "log",
"DaysToKeep": 90,
"Recursive": true
}
]
}
@@ -0,0 +1,14 @@
{
"ComputerName": "itdernappu01.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs",
"Extension": "log",
"DaysToKeep": 90,
"Recursive": true
}
]
}
@@ -0,0 +1,20 @@
{
"ComputerName": "itdscmt1.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs",
"Extension": "log",
"DaysToKeep": 90,
"Recursive": true
},
{
"Path": "C:\\temp",
"Extension": "txt",
"DaysToKeep": 30,
"Recursive": false
}
]
}
@@ -0,0 +1,14 @@
{
"ComputerName": "itdvmvc1script.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\inetpub\\logs\\LogFiles",
"Extension": "log",
"DaysToKeep": 90,
"Recursive": true
}
]
}
@@ -0,0 +1,14 @@
{
"ComputerName": "itdwinautot1.nd.gov",
"NotifyEmail": [
"zmeier@nd.gov"
],
"Directory": [
{
"Path": "C:\\temp",
"Extension": "txt",
"DaysToKeep": 30,
"Recursive": false
}
]
}
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<package>
<metadata>
<id>ITD.ITD-WindowsServer.FileManagement</id>
<version>$VERSIONHERE$</version>
<authors>Zack Meier</authors>
<description>Functions for Windows Server file management</description>
</metadata>
<files>
<file src="**" exclude="**\.git\**;**\Build\**" />
</files>
</package>
@@ -0,0 +1,132 @@
#
# Module manifest for module 'ITD.ITD-WindowsServer.FileManagement'
#
# Generated by: zmeier
#
# Generated on: 6/14/2022
#
@{
# Script module or binary module file associated with this manifest.
RootModule = 'ITD.ITD-WindowsServer.FileManagement.psm1'
# Version number of this module.
ModuleVersion = '<ModuleVersion>'
# Supported PSEditions
CompatiblePSEditions = 'Desktop', 'Core'
# ID used to uniquely identify this module
GUID = '85e7676b-f0e3-4908-aafa-25c9606ca8b7'
# Author of this module
Author = 'zmeier'
# Company or vendor of this module
CompanyName = 'State of North Dakota'
# Copyright statement for this module
Copyright = '(c) zmeier. All rights reserved.'
# Description of the functionality provided by this module
Description = 'Functions for Windows Server file management'
# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(<FunctionsToExport>)
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
# A URL to the license for this module.
# LicenseUri = ''
# A URL to the main website for this project.
# ProjectUri = ''
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfo URI of this module
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
@@ -0,0 +1,23 @@
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
#Dot source the files
Foreach($import in @($Public + $Private))
{
Try
{
. $import.fullname
}
Catch
{
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
# Here I might...
# Read in or create an initial config file and variable
# Export Public functions ($Public.BaseName) for WIP modules
# Set variables visible to the module and its functions only
Export-ModuleMember -Function $Public.Basename
@@ -0,0 +1,26 @@
[
{
"ComputerName": "itdscmt1.nd.gov",
"NotifyEmail": "zmeier@nd.gov",
"Directory": [
{
"Path": "C:\\inetpub\\logs\\LogFiles",
"DaysToKeep": 120,
"Extension": "log",
"Recursive": true
}
]
},
{
"ComputerName": "itdzmtest555.nd.gov",
"NotifyEmail": "zmeier@nd.gov",
"Directory": [
{
"Path": "C:\\windows\\temp",
"DaysToKeep": 99,
"Extension": "log",
"Recursive": false
}
]
}
]
@@ -0,0 +1,22 @@
- ComputerName: itdscmt1.nd.gov
Directory:
- Path: C:\windows\temp
DaysToKeep: 30
Extension: txt
Recursive: false
- ComputerName: itdsccmp2.nd.gov
Directory:
- Path: C:\windows\temp
DaysToKeep: 15
Extension: txt
Recursive: false
- Path: C:\inetpub\logs
DaysToKeep: 120
Extension: txt
Recursive: false
- ComputerName: itdzmtest555.nd.gov
Directory:
- Path: "C:\windows\temp",
DaysToKeep": 99,
Extension": "log",
Recursive": false
@@ -0,0 +1,14 @@
[
{
"Path": "C:\\temp",
"Extension": "log",
"DaysToKeep": 30,
"Recursive": true
},
{
"Path": "C:\\temp2",
"Extension": "txt",
"DaysToKeep": 45,
"Recursive": false
}
]
@@ -0,0 +1,89 @@
<#
.SYNOPSIS
Script will discover all files that are considered "expired".
.DESCRIPTION
Script will discover all files that are considered "expired" based on the parameters in the ./Helpers/ITDExpiredFiles.json files in the ITD-WindowsServer module
.NOTES
Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
.EXAMPLE
Get-ITDExpiredFiles
.EXAMPLE
Get-ITDExpiredFiles -Credential $PrvCred
.EXAMPLE
Get-ITDExpiredFiles -ComputerName itdxyz.nd.gov -Credential $PrvCred
#>
function Get-ITDExpiredFiles {
[CmdletBinding()]
param (
[string]
$ComputerName,
[PSCredential]
$Credential
)
Begin {
Write-Verbose -Message "Load json files into memory"
$JsonFiles = Get-ChildItem -Path "$PSScriptRoot\..\Helpers\*.json" | Where-Object Name -NE ^template.json
$MachineInfo = ForEach ($file in $JsonFiles) {
Get-Content -Path $file.FullName | ConvertFrom-Json
}
If ($PSBoundParameters.ContainsKey('ComputerName')) {
Write-Verbose -Message "ComputerName found"
$MachineInfo = $MachineInfo | Where-Object ComputerName -EQ $ComputerName
}
}
Process {
Write-Verbose -Message "Prep discovery function"
$GetExpiredFilesFunc = {
Write-Verbose -Message ($env:COMPUTERNAME + " " + $args[0] + " " + $args[1] + " " + $args[2])
$GetChildItemParams = @{
Path = $args[0];
Filter = $args[1];
Recurse = $args[2];
}
$FilesFound = (Get-ChildItem @GetChildItemParams | Where-Object LastWriteTime -LT ((Get-Date).AddDays(-$args[3])))
Write-Output $FilesFound
}
$GetITDExpiredFilesResult = [System.Collections.ArrayList]@()
ForEach ($Server in $MachineInfo) {
Write-Verbose -Message ("Start " + $Server.ComputerName)
Write-Verbose -Message "Ping test before any Invoke-Command"
If ((Test-NetConnection -ComputerName $Server.ComputerName).PingSucceeded) {
ForEach ($Directory in $Server.Directory) {
Write-Verbose -Message ("Start " + $server.ComputerName + " " + $Directory.Path)
$FilesFoundOnServer = $null
$InvokeCommandParams = $null
$InvokeResult = $null
$InvokeCommandParams = @{
ComputerName = $Server.ComputerName;
Credential = $Credential;
ScriptBlock = $GetExpiredFilesFunc;
ArgumentList = @($Directory.Path, ("*" + $Directory.Extension), $Directory.Recursive, $Directory.DaysToKeep);
}
$FilesFoundOnServer = Invoke-Command @InvokeCommandParams
Write-Output $FilesFoundOnServer
#$null = $GetITDExpiredFilesResult.Add($FilesFoundOnServer)
Write-Verbose -Message ("End " + $server.ComputerName + " " + $Directory.Path)
}
}
Else {
Write-Error -Message ($Server.ComputerName + " ping test failed, generate ticket someday.")
}
}
Write-Verbose -Message ("End " + $server.ComputerName)
}
End {
#Write-Output $GetITDExpiredFilesResult
Write-Verbose -Message "End Get-ITDExpiredFiles"
}
}
@@ -0,0 +1,99 @@
function Remove-ITDExpiredFiles {
[CmdletBinding()]
param (
[string]
$ComputerName,
[switch]
$WhatIf,
[PSCredential]
$Credential
)
Begin {
Write-Verbose -Message "Start Remove-ITDExpiredFiles"
}
Process {
$FilesRemovedSuccess = @()
$FilesRemovedFailure = @()
$GetITDExpiredFilesAutoParams += @{}
If ($PSBoundParameters.ContainsKey('ComputerName')) {
Write-Verbose -Message "ComputerName parameter found"
$GetITDExpiredFilesParams += @{
ComputerName = $ComputerName;
}
}
If ($PSBoundParameters.ContainsKey('Credential')) {
Write-Verbose -Message "Credential parameter found"
$GetITDExpiredFilesParams += @{
Credential = $Credential;
}
}
$FilesToRemove = Get-ITDExpiredFiles @GetITDExpiredFilesParams
Write-Verbose -Message ("Found " + $FilesToRemove.count + " expired files to remove")
ForEach ($File in $FilesToRemove) {
Write-Verbose -Message ("Start~" + $File.PSComputerName + "~" + $File.FullName )
$InvokeCommandParams = @{
ComputerName = $File.PSComputerName;
Credential = $Credential;
ErrorAction = 'Stop';
ArgumentList = @($File.FullName);
ScriptBlock = { Get-Item -Path $args[0] | Remove-Item }
}
switch ($WhatIf) {
$true {
try {
Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " removed")
Write-Host -Message ($Server.ComputerName + " -- " + 'What if: Performing the operation "Remove File" on target ' + $File.FullName)
# log success
$FilesRemovedSuccess += [PSCustomObject]@{
ComputerName = $File.PSComputerName;
Name = $File.Fullname;
Timestamp = (Get-Date).tostring("yyyy/MM/dd HH:mm:ss")
}
Write-Output $File
}
catch {
Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " failure")
# log failure
$FilesRemovedFailure += [PSCustomObject]@{
ComputerName = $File.PSComputerName;
Name = $File.Fullname;
Timestamp = (Get-Date).tostring("yyyy/MM/dd HH:mm:ss")
}
}
}
Default {
try {
Invoke-Command @InvokeCommandParams
Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " removed")
# log success
$FilesRemovedSuccess += [PSCustomObject]@{
ComputerName = $File.PSComputerName;
Name = $File.Fullname;
Timestamp = (Get-Date).tostring("yyyy/MM/dd HH:mm:ss")
}
Write-Output $File
}
catch {
Write-Verbose -Message ("Start~" + $File.PSComputerName + "~" + $File.FullName + " failure")
# log failure
$FilesRemovedFailure += [PSCustomObject]@{
ComputerName = $File.PSComputerName;
Name = $File.Fullname;
Timestamp = (Get-Date).tostring("yyyy/MM/dd HH:mm:ss")
}
}
}
}
Write-Verbose -Message ("End~" + $File.PSComputerName + "~" + $File.FullName )
}
}
End {
Write-Verbose -Message "End Remove-ITDExpiredFilesAuto"
}
}
@@ -0,0 +1,20 @@
# Introduction
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
# Getting Started
TODO: Guide users through getting your code up and running on their own system. In this section you can talk about:
1. Installation process
2. Software dependencies
3. Latest releases
4. API references
# Build and Test
TODO: Describe and show how to build your code and run the tests.
# Contribute
TODO: Explain how other users and developers can contribute to make your code better.
If you want to learn more about creating good readme files then refer the following [guidelines](https://docs.microsoft.com/en-us/azure/devops/repos/git/create-a-readme?view=azure-devops). You can also seek inspiration from the below readme files:
- [ASP.NET Core](https://github.com/aspnet/Home)
- [Visual Studio Code](https://github.com/Microsoft/vscode)
- [Chakra Core](https://github.com/Microsoft/ChakraCore)