mirror of
https://github.com/serialexperiments0815/PowershellExcercises.git
synced 2026-11-07 12:55:30 +00:00
Expanded and structurized scripts.
This commit is contained in:
parent
361e384195
commit
fbcf753965
33 changed files with 158 additions and 0 deletions
|
|
@ -0,0 +1,14 @@
|
|||
# Opens notepad process/program if it is not in the running Process list.
|
||||
# processName variable for easy exchange of process
|
||||
# Forms Messagebox instead of Write-Host for more urgency
|
||||
|
||||
$processName = "notepad"
|
||||
|
||||
while ($true){
|
||||
if (-not (Get-Process -name $processName -ErrorAction SilentlyContinue)){
|
||||
[System.Windows.Forms.MessageBox]::Show("$processName is not running! It will now be opened.", "Alert",
|
||||
[System.Windows.Forms.MessageBoxButtons]::Ok,
|
||||
[System.Windows.Forms.MessageBoxIcon]::Warning)
|
||||
Start-Process $processName
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
# Creates downloads Folder and uses the Invoke-WebRequest function to acquire and download file to said folder.
|
||||
# System.IO.Path is called to extract Filename from URL.
|
||||
# Microsoft.VisualBasic.Interaction is called to provide simple input dialog.
|
||||
|
||||
$downloadFolder = "$($PSScriptRoot)/downloads"
|
||||
|
||||
if (-not (Test-Path $downloadFolder)){
|
||||
New-Item -Path $downloadFolder -ItemType Directory
|
||||
}
|
||||
|
||||
Add-Type -AssemblyName Microsoft.VisualBasic
|
||||
$userInput = [Microsoft.VisualBasic.Interaction]::InputBox("Enter URL: ", "downloadFilesFromURL", "")
|
||||
# [System.Windows.Forms.MessageBox]::Show("Your entered URL is '$userInput'.", "Alert",
|
||||
# [System.Windows.Forms.MessageBoxButtons]::Ok,
|
||||
# [System.Windows.Forms.MessageBoxIcon]::Information)
|
||||
|
||||
if (-not $userInput -or -not ($userInput -match "^https?://")){
|
||||
[System.Windows.Forms.MessageBox]::Show("Incorrect URL : $userInput", "Alert",
|
||||
[System.Windows.Forms.MessageBoxButtons]::Ok,
|
||||
[System.Windows.Forms.MessageBoxIcon]::Error)
|
||||
return
|
||||
}
|
||||
|
||||
$fileName = [System.IO.Path]::GetFileName($userInput)
|
||||
$downloadFolderPath = Join-Path $downloadFolder $fileName
|
||||
Invoke-WebRequest -Uri $userInput -OutFile $downloadFolderPath -ErrorAction Stop
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
# Exports CSV file containing all installed programms
|
||||
|
||||
# Test-Path added to avoid Remove-Item error on first execution.
|
||||
# Remove-Item was added to avoid complications with Export-Csv function.
|
||||
# Delimiter was added to not have all properties stacked in the same column.
|
||||
if (Test-Path "./InstalledPrograms.csv"){
|
||||
Remove-Item -Path "./InstalledPrograms.csv"
|
||||
}
|
||||
Get-Package | Select-Object -Property Name, Version, Summary |
|
||||
Export-Csv -Path "./InstalledPrograms.csv" -NoTypeInformation -Delimiter ";"
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
# File creates listeners for happenings folder
|
||||
# Listens for file creation, deletion, renaming
|
||||
# Exports events to log.txt
|
||||
|
||||
# $fsw to save typing for three listeners.
|
||||
# Property @{IncludeSubdirectories = $true} to trigger listeners in subdirectories too.
|
||||
# EnableRaisingEvents = $true explicitely enabled for completeness sake, by default powershell implicitely turns it on.
|
||||
$fsw = New-Object IO.FileSystemWatcher "$($PSScriptRoot)/happenings/" -Property @{
|
||||
IncludeSubdirectories = $true
|
||||
EnableRaisingEvents = $true}
|
||||
|
||||
Register-ObjectEvent $fsw Created -SourceIdentifier "fileFolderCreated" -Action {
|
||||
$name = $Event.SourceEventArgs.Name
|
||||
$fullPath = $Event.SourceEventArgs.FullPath
|
||||
|
||||
Add-Content "./log.txt" -value "[FILE CREATION]: DATE: $(Get-Date), NAME: $name, PATH: $fullPath"
|
||||
}
|
||||
|
||||
Register-ObjectEvent $fsw Deleted -SourceIdentifier "fileFolderDeleted" -Action {
|
||||
$name = $Event.SourceEventArgs.Name
|
||||
$fullPath = $Event.SourceEventArgs.FullPath
|
||||
|
||||
Add-Content "./log.txt" -value "[FILE DELETION]: DATE: $(Get-Date), NAME: $name, PATH: $fullPath"
|
||||
}
|
||||
|
||||
Register-ObjectEvent $fsw Renamed -SourceIdentifier "fileFolderRenamed" -Action {
|
||||
$oldName = $Event.SourceEventArgs.OldName
|
||||
$oldFullPath = $Event.SourceEventArgs.OldFullPath
|
||||
$name = $Event.SourceEventArgs.Name
|
||||
$fullPath = $Event.SourceEventArgs.FullPath
|
||||
|
||||
Add-Content "./log.txt" -value "[FILE RENAMING]: DATE: $(Get-Date), OLDNAME: $oldName, OLDPATH: $oldFullPath, NEWNAME: $name, NEWPATH: $fullPath"
|
||||
}
|
||||
|
||||
# See active listeners
|
||||
# Get-EventSubscriber -Force | Format-Table -AutoSize
|
||||
|
||||
# Remove all active listeners
|
||||
# Get-EventSubscriber -Force | Unregister-Event -Force
|
||||
Loading…
Add table
Add a link
Reference in a new issue