Why the argument is empty or null - windows

everyone
I'm a novice at Powershell.
My script gather Windows Event Viewer logs and export data to csv files. If the input is empty then is used the default values.
But it crashes:
"Export-Csv: Unable to validate argument for "Path" parameter. The argument is empty or NULL. Specify a non-empty, non-null argument and then run the command again."
Could explain to me why??
function exportLogs
{
[CmdletBinding()]
param (
$exportFolder=(Read-Host -prompt "exportFolder"),
$applicatiomLogs=(Read-Host -prompt "applicationLogs"),
$systemLogs=(Read-Host -prompt "systemLogs"),
$fromDate=(Read-Host -prompt "fromDate"),
$eventTypes=(Read-Host -prompt "eventTypes")
)
$comp = $env:computername
$now=Get-Date
#check empty input and assign default values
if (!$exportFolder) { [String]$exportFolder="D:\temp\" }
if (!$applicationLogs) {
$applicationLogs="True"
[System.Convert]::ToBoolean($applicationLogs)
}
if (!$systemLogs) {
$systemLogs="True"
[System.Convert]::ToBoolean($systemLogs)
}
if (!$fromDate) { [DateTime]$fromDate=$now.AddDays(-30) }
if (!$eventtypes) { [String[]]$eventtypes=("Error", "Warning") }
if ($applicationLogs)
{
$exportFile = $exportFolder + "applicationLogs_" + $now.ToString("yyyyMMddHHmmss") + ".csv"
$exportedLog = get-eventlog -ComputerName $comp -log "Application" -After $fromDate -EntryType $eventTypes
exportCsv($exportedLog, $exportFile)
}
if ($systemLogs)
{
$exportFile = $exportFolder + "systemLogs_" + $now.ToString("yyyyMMddHHmmss") + ".csv"
$exportedLog = get-eventlog -ComputerName $comp -log "System" -After $fromDate -EntryType $eventTypes
exportCsv($exportedLog, $exportFile)
}
}
function exportCsv([String]$exportedLog, [String]$exportFile)
{
$el_sorted = $exportedLog | Sort-Object TimeGenerated
Write-Host Exporting to $exportFile
$el_sorted|Select MachineName, TimeGenerated, EntryType, Source, Message | Export-CSV $exportFile -NoTypeInfo
}

Try this refactor... (run on the default Windows Sandbox on Win10)
function New-LogExport
{
Param
(
[String]$exportedLog,
[String]$exportFile
)
$el_sorted = $exportedLog |
Sort-Object TimeGenerated
Write-Host "Exporting to $exportFile"
$el_sorted |
Select-Object MachineName,
TimeGenerated,
EntryType,
Source,
Message |
Export-Csv -Path $exportFile -NoTypeInfo
}
function Start-ExportLogs
{
[CmdletBinding()]
param
(
$exportFolder = (Read-Host -prompt 'exportFolder'),
$applicatiomLogs = (Read-Host -prompt 'applicationLogs'),
$systemLogs = (Read-Host -prompt 'systemLogs'),
$fromDate = (Read-Host -prompt 'fromDate'),
$eventTypes = (Read-Host -prompt 'eventTypes')
)
$comp = $env:computername
$now = Get-Date
if (!$exportFolder)
{[String]$exportFolder = 'C:\temp'}
if (!$applicationLogs)
{
$applicationLogs = 'True'
[System.Convert]::ToBoolean($applicationLogs)
}
if (!$systemLogs)
{
$systemLogs = 'True'
[System.Convert]::ToBoolean($systemLogs)
}
if (!$fromDate)
{[DateTime]$fromDate = $now.AddDays(-30)}
if (!$eventtypes)
{[String[]]$eventtypes = ('Error', 'Warning')}
if ($applicationLogs)
{
$exportFile = "$exportFolder\applicationLogs_$($now.ToString('yyyyMMddHHmmss')).csv"
$getEventLogSplat = #{
ComputerName = $comp
LogName = 'Application'
EntryType = $eventTypes
After = $fromDate
}
$exportedLog = Get-EventLog #getEventLogSplat
New-LogExport -exportedLog $exportedLog, -exportFile $exportFile
}
if ($systemLogs)
{
$exportFile = "$exportFolder\systemLogs_$($now.ToString('yyyyMMddHHmmss')).csv"
$getEventLogSplat = #{
ComputerName = $comp
LogName = 'System'
EntryType = $eventTypes
After = $fromDate
}
$exportedLog = Get-EventLog #getEventLogSplat
}
New-LogExport -exportedLog $exportedLog -exportFile $exportFile
}
# Results
<#
exportFolder:
applicationLogs:
systemLogs:
fromDate:
eventTypes:
True
True
Exporting to C:\temp\applicationLogs_20210120143659.csv
Exporting to C:\temp\systemLogs_20210120143659.csv
#>

Related

How to get the 'status' column value in Windows explorer by PowerShell or java

when we open a windows explorer, we will see multi-column, like this:
now, I want to get the status column(Circled in red) value by PowerShell or java, Is there a way to do it?
You can include below function and modify as per your need in Powershell.
Below Powershell function can be used to get all details or properties of an item in file explorer.
function Get-FileMetaData {
<#
.SYNOPSIS
Small function that gets metadata information from file providing similar output to what Explorer shows when viewing file
.DESCRIPTION
Small function that gets metadata information from file providing similar output to what Explorer shows when viewing file
.PARAMETER File
FileName or FileObject
.EXAMPLE
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force | Get-FileMetaData | Out-HtmlView -ScrollX -Filtering -AllProperties
.EXAMPLE
Get-ChildItem -Path $Env:USERPROFILE\Desktop -Force | Where-Object { $_.Attributes -like '*Hidden*' } | Get-FileMetaData | Out-HtmlView -ScrollX -Filtering -AllProperties
.NOTES
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, ValueFromPipeline)][Object] $File,
[switch] $Signature
)
Process {
foreach ($F in $File) {
$MetaDataObject = [ordered] #{}
if ($F -is [string]) {
$FileInformation = Get-ItemProperty -Path $F
} elseif ($F -is [System.IO.DirectoryInfo]) {
#Write-Warning "Get-FileMetaData - Directories are not supported. Skipping $F."
continue
} elseif ($F -is [System.IO.FileInfo]) {
$FileInformation = $F
} else {
Write-Warning "Get-FileMetaData - Only files are supported. Skipping $F."
continue
}
$ShellApplication = New-Object -ComObject Shell.Application
$ShellFolder = $ShellApplication.Namespace($FileInformation.Directory.FullName)
$ShellFile = $ShellFolder.ParseName($FileInformation.Name)
$MetaDataProperties = [ordered] #{}
0..400 | ForEach-Object -Process {
$DataValue = $ShellFolder.GetDetailsOf($null, $_)
$PropertyValue = (Get-Culture).TextInfo.ToTitleCase($DataValue.Trim()).Replace(' ', '')
if ($PropertyValue -ne '') {
$MetaDataProperties["$_"] = $PropertyValue
}
}
foreach ($Key in $MetaDataProperties.Keys) {
$Property = $MetaDataProperties[$Key]
$Value = $ShellFolder.GetDetailsOf($ShellFile, [int] $Key)
if ($Property -in 'Attributes', 'Folder', 'Type', 'SpaceFree', 'TotalSize', 'SpaceUsed') {
continue
}
If (($null -ne $Value) -and ($Value -ne '')) {
$MetaDataObject["$Property"] = $Value
}
}
if ($FileInformation.VersionInfo) {
$SplitInfo = ([string] $FileInformation.VersionInfo).Split([char]13)
foreach ($Item in $SplitInfo) {
$Property = $Item.Split(":").Trim()
if ($Property[0] -and $Property[1] -ne '') {
$MetaDataObject["$($Property[0])"] = $Property[1]
}
}
}
$MetaDataObject["Attributes"] = $FileInformation.Attributes
$MetaDataObject['IsReadOnly'] = $FileInformation.IsReadOnly
$MetaDataObject['IsHidden'] = $FileInformation.Attributes -like '*Hidden*'
$MetaDataObject['IsSystem'] = $FileInformation.Attributes -like '*System*'
if ($Signature) {
$DigitalSignature = Get-AuthenticodeSignature -FilePath $FileInformation.Fullname
$MetaDataObject['SignatureCertificateSubject'] = $DigitalSignature.SignerCertificate.Subject
$MetaDataObject['SignatureCertificateIssuer'] = $DigitalSignature.SignerCertificate.Issuer
$MetaDataObject['SignatureCertificateSerialNumber'] = $DigitalSignature.SignerCertificate.SerialNumber
$MetaDataObject['SignatureCertificateNotBefore'] = $DigitalSignature.SignerCertificate.NotBefore
$MetaDataObject['SignatureCertificateNotAfter'] = $DigitalSignature.SignerCertificate.NotAfter
$MetaDataObject['SignatureCertificateThumbprint'] = $DigitalSignature.SignerCertificate.Thumbprint
$MetaDataObject['SignatureStatus'] = $DigitalSignature.Status
$MetaDataObject['IsOSBinary'] = $DigitalSignature.IsOSBinary
}
[PSCustomObject] $MetaDataObject
}
}
}
Reference :
Follow Below Link for the complete tutorial.
https://evotec.xyz/getting-file-metadata-with-powershell-similar-to-what-windows-explorer-provides/

Server Pending Reboot

I am trying to modify my PowerShell script to find the best possible ways to check for Pending Reboots on our servers. This script checks the registry entries. However, I am seeing inconsistencies from other PowerShell scripts and wanting guidance on the best approach.
function PendingReboot ($comp) {
process {
try {
$WMI_OS = ""
$RegCon = ""
$WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $comp -ErrorAction Stop
if ($?){
try{
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$comp)
If ($WMI_OS.BuildNumber -ge 6001){
$RegValueSetupex = ""
$RegValuePFRO2k8 = ""
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValueSetupex = $RegSubKeySM.GetValue("SetupExecute",$null)
if ($RegValueSetupex){
$RegValueSetupex = $true
}
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO2k8 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)
if ($RegValuePFRO2k8 ){
$RegValuePFRO2k8 = $true
}
$RegCon.Close()
if ( $RegValueSetupex -eq $true -or $RegValuePFRO2k8 -eq $true){
return '<font color="#FF0000">'+$true
}
else {
return $false
}
}
else{
$RegValuePFRO2k3 = $false;
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine","$comp")
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO2k3 = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)
$RegCon.Close()
If ($RegValuePFRO2k3) {
return '<font color="#FF0000">'+$true;
}
else {
return $false;
}
}
}
catch {
return '<font color="#FFFF00">'+"Remote Registry Service KO"
}
}
else {
throw $error[0].Exception
}
}
catch {
return '<font color="#FF0000">'+"RPC Issue"
}
}
}
Try this.
function PendingBoot($comp) {
$pendingRebootTests = #(
#{
Name = 'RebootPending'
Test = { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' Name 'RebootPending' -ErrorAction Ignore }
TestType = 'ValueExists'
}
#{
Name = 'RebootRequired'
Test = { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' Name 'RebootRequired' -ErrorAction Ignore }
TestType = 'ValueExists'
}
#{
Name = 'PendingFileRenameOperations'
Test = { Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction Ignore }
TestType = 'NonNullValue'
}
)
$session = New-PSSession -Computer SRV1
foreach ($test in $pendingRebootTests) {
$result = Invoke-Command -Session $session -ScriptBlock $test.Test
if ($test.TestType -eq 'ValueExists' -and $result) {
$true
} elseif ($test.TestType -eq 'NonNullValue' -and $result -and $result.($test.Name)) {
$true
} else {
$false
}
}
$session | Remove-PSSession
}

Oracle & Powershell different behaviour in batch file

I have built a script that connects to an Oracle database and proceed to some various export.
It works perfectly when I launch powershell.exe from Windows start menu and run the script as follows:
.\OracleConnect
But, when I try to automatize the exact same script by launching it from a batch, I'm getting an error message:
"The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception"
As I said the script is identical in both case.
My .bat file is as follows:
set path=%~dp0
set file=OracleConnect.ps1
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file%
I've also tried with the alternative powershell.exe path:
C:\Windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass %file%
Without any success.
Here is my full script:
$scriptpath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Set-Location $scriptpath
[void][Reflection.Assembly]::LoadFile($scriptpath + "\Oracle.DataAccess.dll")
$logpath = $scriptpath + "\Export.txt"
$connectionpath = read-host 'connection file [optional]'
if($connectionpath)
{
$connectionstring = get-content $connectionpath
}
else
{
$server = read-host 'host name'
$user = read-host 'user name'
$pass = read-host 'password'
$database = read-host 'service'
$connectionstring = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=$server)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=$database)));User Id=$user;Password=$pass;"
}
$echoon = read-host 'echo on (y - n) [default off]'
function global:get-result($query)
{
$oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
$oracleconnection.connectionstring = $connectionstring
$oracleconnection.Open()
$oraclecommand = $oracleconnection.CreateCommand()
$oraclecommand.CommandText = $query
$reader = $oraclecommand.ExecuteReader()
$writer = new-object System.IO.StreamWriter($logpath)
$counter = 0
write-output ""
while ($reader.Read())
{
$columns = ""
$columnslog = ""
$tab = ""
$tablog = ""
if($counter -eq 0)
{
for ($i= 0; $i -lt $reader.FieldCount; $i++)
{
$columns = $columns + " | " + $reader.GetName($i)
$columnslog = $columnslog + "`t" + $reader.GetName($i).ToUpper()
}
$writer.WriteLine($columnslog)
if($echoon -eq "y")
{
write-output $columns
write-output ""
}
}
for ($i= 0; $i -lt $reader.FieldCount; $i++)
{
$tab = $tab + " | " + $reader.GetValue($i).ToString()
$tablog = $tablog + "`t" + $reader.GetValue($i).ToString()
}
$writer.WriteLine($tablog)
if($echoon -eq "y")
{
write-output $tab
write-output ""
}
$counter = $counter + 1
}
$oracleconnection.Close()
$writer.Close()
}
function global:update-query($query)
{
$oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
$oracleconnection.connectionstring = $connectionstring
$oracleconnection.Open()
$oraclecommand = $oracleconnection.CreateCommand()
$oraclecommand.CommandText = $query
$reader = $oraclecommand.ExecuteNonQuery()
}
function global:readscript($path)
{
$inenc = [System.Text.Encoding]::UTF7
$reader = new-object System.IO.StreamReader($path, $inenc)
$finalquery = ""
while ($line = $reader.ReadLine())
{
$finalquery += $line
}
$reader.close()
return $finalquery
}
write-output "Enter query or type 'exit' to quit..."
while($true)
{
$value = read-host " "
if($value -eq "exit")
{
$save = read-host "Save connection ? [Y:yes N:no]"
if($save.ToLower() -eq "y")
{
$path = read-host "Connection path"
if(test-path $path)
{
rm $path
}
new-item -type file -force $path
$writer = New-Object System.IO.StreamWriter $path
$writer.WriteLine($connectionstring)
$writer.Close()
}
else
{
}
return
}
else
{
if($value.Tolower().EndsWith(".sql") -eq $false)
{
if ($value.ToLower().StartsWith("select") -or $value.ToLower().StartsWith("show"))
{
get-result($value)
}
else
{
update-query($value)
}
}
else
{
$scriptquery = readscript $value
if ($scriptquery.ToLower().StartsWith("select") -or $scriptquery.ToLower().StartsWith("show"))
{
get-result($scriptquery)
}
else
{
update-query($scriptquery)
}
}
}
}
Edit:
I've now tried to catch the exception adding a try catch instruction here:
try
{
$oracleconnection = new-object Oracle.DataAccess.Client.OracleConnection
}
catch
{
write-host $_.Exception.ToString()
}
And I'm getting this message:
Oracle.DataAccess.Client.OracleException: The provider is not compatible with the version of Oracle client
I still don't understand how this can be an issue with the version of Oracle since it works fine when I run it from a powershell window.

Get free disk space for different servers with separate credentials

I'm trying to query Disk space info on Grid box and Export it to CSV.
I was able to use Marc Weisel's script to make it use different credentials. I used computers.csv
to store the computer name and referenced credentials. This is working although the problem I'm facing is that Grid box opens for all the servers mentioned in the csv.
I want to view all disk space information on the same grid box.
Below are my script and module I'm using
Import-Module D:\get.psm1
Import-Module D:\out-CSV.psm1
$ComputerList = Import-Csv -Path d:\Computers.csv;
#$servers = 'laptop-pc','laptop-pc','laptop-pc'
$CredentialList = #{
Cred1 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'laptop-pc\laptop', (ConvertTo-SecureString -String 'tamboli' -AsPlainText -Force);
#Cred2 = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'tnt\administrator', (ConvertTo-SecureString -String 'Atlantic12' -AsPlainText -Force);
}
foreach ($computer in $ComputerList )
{
Get-DiskFree -ComputerName $Computer.Name -Credential $CredentialList[$Computer.Credential] -Format | ? { $_.Type -like '*fixed*' } | select * -ExcludeProperty Type |
}
get.ps1
function Get-DiskFree
{
[CmdletBinding()]
param
(
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Alias('hostname')]
[Alias('cn')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Position=1,
Mandatory=$false)]
[Alias('runas')]
[System.Management.Automation.Credential()]$Credential =
[System.Management.Automation.PSCredential]::Empty,
[Parameter(Position=2)]
[switch]$Format
)
BEGIN
{
function Format-HumanReadable
{
param ($size)
switch ($size)
{
{$_ -ge 1PB}{"{0:#.#'P'}" -f ($size / 1PB); break}
{$_ -ge 1TB}{"{0:#.#'T'}" -f ($size / 1TB); break}
{$_ -ge 1GB}{"{0:#.#'G'}" -f ($size / 1GB); break}
{$_ -ge 1MB}{"{0:#.#'M'}" -f ($size / 1MB); break}
{$_ -ge 1KB}{"{0:#'K'}" -f ($size / 1KB); break}
default {"{0}" -f ($size) + "B"}
}
}
$wmiq = 'SELECT * FROM Win32_LogicalDisk WHERE Size != Null AND DriveType >= 2'
}
PROCESS
{
foreach ($computer in $ComputerName)
{
try
{
if ($computer -eq $env:COMPUTERNAME)
{
$disks = Get-WmiObject -Query $wmiq `
-ComputerName $computer -ErrorAction Stop
}
else
{
$disks = Get-WmiObject -Query $wmiq `
-ComputerName $computer -Credential $Credential `
-ErrorAction Stop
}
if ($Format)
{
# Create array for $disk objects and then populate
$diskarray = #()
$disks | ForEach-Object { $diskarray += $_ }
$diskarray | Select-Object #{n='Name';e={$_.SystemName}},
#{n='Vol';e={$_.DeviceID}},
#{n='Size';e={Format-HumanReadable $_.Size}},
#{n='Used';e={Format-HumanReadable `
(($_.Size)-($_.FreeSpace))}},
#{n='Avail';e={Format-HumanReadable $_.FreeSpace}},
#{n='Use%';e={[int](((($_.Size)-($_.FreeSpace))`
/($_.Size) * 100))}},
#{n='FS';e={$_.FileSystem}},
#{n='Type';e={$_.Description}}
}
else
{
foreach ($disk in $disks)
{
$diskprops = #{'Volume'=$disk.DeviceID;
'Size'=$disk.Size;
'Used'=($disk.Size - $disk.FreeSpace);
'Available'=$disk.FreeSpace;
'FileSystem'=$disk.FileSystem;
'Type'=$disk.Description
'Computer'=$disk.SystemName;}
# Create custom PS object and apply type
$diskobj = New-Object -TypeName PSObject `
-Property $diskprops
$diskobj.PSObject.TypeNames.Insert(0,'BinaryNature.DiskFree')
Write-Output $diskobj
}
}
}
catch
{
# Check for common DCOM errors and display "friendly" output
switch ($_)
{
{ $_.Exception.ErrorCode -eq 0x800706ba } `
{ $err = 'Unavailable (Host Offline or Firewall)';
break; }
{ $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } `
{ $err = 'Access denied (Check User Permissions)';
break; }
default { $err = $_.Exception.Message }
}
Write-Warning "$computer - $err"
}
}
}
END {}
}
You need to append the result of Get-DiskFree to an array in the loop, and pipe that result to the Grid View from outside your loop:
foreach ($computer in $ComputerList )
{
$result += Get-DiskFree -ComputerName $Computer.Name -Credential $CredentialList[$Computer.Credential] -Format | ? { $_.Type -like '*fixed*' } | select * -ExcludeProperty Type
}
$result | Out-GridView
Or, better yet, pipe the result from outside the Foreach-Object loop:
$ComputerList | % {
Get-DiskFree -ComputerName $_.Name -Credential $CredentialList[$_.Credential] -Format | ? { $_.Type -like '*fixed*' } | select * -ExcludeProperty Type
} | Out-GridView

Powershell input validition

I am trying to only allow a user to enter 2 letters when they rename a file if its exists.
It is not working as I wanted it. I am allowed to enter numbers.
if ($RenameLargeFiles.Length -gt '2') {
Write-Host " You may only enter 2 letters."
Pause
Clear-Host
Rename-LargeFiles
} else {
Rename-Item -Path $LargeFiles $RenameLargeFiles".txt"
$LargeFiles = $RenameLargeFiles
Set-Content -Value $Files -Path $LargeFiles
}
}
Set-StrictMode –Version Latest
$LargeFiles = "C:\PSScripts\LargeFiles.txt"
$PSScripts = "C:\PSScripts"
$TestPSScripts = Test-Path "C:\PSScripts"
switch ($TestPSScripts) {
'False' { New-Item -Type directory -Path $PSScripts }
}
function Test-Files {
$Files8 = ""
$Files8 = Read-Host " Please Enter the Complete Path. "
$TFiles8 = Test-Path $files8
if ($TFiles8 -eq 'True') {
Write-Host $Files8 "LOCATED."
} else {
Write-Host $Files8 "NOT FOUND"
Pause
Clear-Host
Test-Files
}
}
function Test-LargeFiles {
$LargeFiles = "C:\PSScripts\LargeFiles.txt"
$TestLargeFiles = Test-Path $LargeFiles
if ($TestLargeFiles -eq 'True') {
Write-Host $LargeFiles "already present"
Rename-LargeFiles
} else {
Write-Host $LargeFiles "NOT FOUND."
Write-Host $LargeFiles "created"
}
}
function Rename-LargeFiles {
$LargeFiles = "C:\PSScripts\LargeFiles.txt"
[string] $RenameLargeFiles = Read-Host " Please Enter 2 letters to rename" $LargeFiles
if ($RenameLargeFiles.Length -gt '2') {
Write-Host " You may only enter 2 letters."
Pause
Clear-Host
Rename-LargeFiles
} else {
Rename-Item -Path $LargeFiles $RenameLargeFiles".txt"
$LargeFiles = $RenameLargeFiles
Set-Content -Value $Files -Path $LargeFiles
}
}
Test-Files
$Files = Get-ChildItem -Recurse $Files8 | Sort-Object -Descending -Property Length | Select-Object -First 10
Test-LargeFiles
Add-Content -Value $Files -Path $LargeFiles
pause
Use a regex like so:
if ($RenameLargeFiles -notmatch '[aA-zZ]{2}') {
... display error message
}

Resources