Powershell - Variable problems - powershell

I wrote a script that will pull data from a .properties file (basically a config file). Some of the data from the properties file has environment data (i.e. %UserProfile%), so I run it through a function (Resolve–EnvVariable) that will replace the environment variable with the actual value. The replace works perfectly, but somehow the data seems to be altered.
When I try to use the values that have been run through the function, they no longer work (see results down below).
This is the file contents of c:\work\test.properties
types="*.txt"
in="%UserProfile%\Downloads"
This is my PowerShell Script
Clear-Host
#Read the properties file and replace the parameters when specified
if (Test-Path C:\work\test.properties) {
$propertiesFile = Get-Content C:\work\test.properties
Write-Host "Parameters will be substituded from properties file" -ForegroundColor Yellow
foreach ($line in $propertiesFile) {
Write-Host ("from Properties file $line")
$propSwitch = $line.Split("=")[0]
$propValue = Resolve–EnvVariable($line.Split("=")[1])
switch ($propSwitch) {
"types" { $types = $propValue }
"in" { $in = $propValue }
}
}
}
write-host ("After running through function `n in=" + $in + "<- types=" + $types + "<-")
# This function resolves environment variables
Function Resolve–EnvVariable {
[cmdletbinding()]
Param(
[Parameter(Position = 0, ValueFromPipeline = $True, Mandatory = $True,
HelpMessage = "Enter string with env variable i.e. %APPDATA%")]
[ValidateNotNullOrEmpty()]
[string]$String
)
Begin {
Write-Verbose "Starting $($myinvocation.mycommand)"
} #Begin
Process {
#if string contains a % then process it
if ($string -match "%\S+%") {
Write-Verbose "Resolving environmental variables in $String"
#split string into an array of values
$values = $string.split("%") | Where-Object { $_ }
foreach ($text in $values) {
#find the corresponding value in ENV:
Write-Verbose "Looking for $text"
[string]$replace = (Get-Item env:$text -erroraction "SilentlyContinue").Value
if ($replace) {
#if found append it to the new string
Write-Verbose "Found $replace"
$newstring += $replace
}
else {
#otherwise append the original text
$newstring += $text
}
} #foreach value
Write-Verbose "Writing revised string to the pipeline"
#write the string back to the pipeline
Write-Output $NewString
} #if
else {
#skip the string and write it back to the pipeline
Write-Output $String
}
} #Process
End {
Write-Verbose "Ending $($myinvocation.mycommand)"
} #End
} #end Resolve-EnvVariable
# Hardcoded values work
$test1 = Get-ChildItem -Path "C:\Users\Paul\Downloads" -Recurse -Include "*.txt"
# Values pulled and updated through function do not work
$test2 = Get-ChildItem -Path $in -Recurse -Include $types
# If I manually assign the values, it works
$in = "C:\Users\Paul\Downloads"
$types = "*.txt"
$test3 = Get-ChildItem -Path $in -Recurse -Include $types
foreach ($test in $test1) { write-host "test1 $test" }
foreach ($test in $test2) { write-host "test2 $test" }
foreach ($test in $test3) { write-host "test3 $test" }
Results
Parameters will be substituded from properties file
from Properties file types="*.txt"
from Properties file in="%UserProfile%\Downloads"
After running through function
in="C:\Users\Paul\Downloads"<- types="*.txt"<-
test1 C:\Users\Paul\Downloads\Test\testPaul.txt
test1 C:\Users\Paul\Downloads\Test2\File1.txt
test3 C:\Users\Paul\Downloads\Test\testPaul.txt
test3 C:\Users\Paul\Downloads\Test2\File1.txt

Two alternatives:
1. Use Environment.ExpandEnvironmentVariables()
If you switched to non-qualified string values and escaped your \, it would be as simple as piping the file to ConvertFrom-StringData, at which point you could expand the variable values with Environment.ExpandEnvironmentVariables():
Properties file:
types=*.txt
in=%UserProfile%\\Downloads
Script:
# Convert file to hashtable
$properties = Get-Content file.properties -Raw |ConvertFrom-StringData
# Copy value to new hashtable, but expand env vars first
$expanded = #{}
foreach($entry in $properties.GetEnumerator()){
$expanded[$entry.Key] = [Environment]::ExpandEnvironmentVariables($entry.Value)
}
Should give you the desired values:
PS C:\> $expanded
Name Value
---- -----
in C:\Users\username\Downloads
types *.txt
2. Use and dot-source a PowerShell script for your properties
This is lifted straight out of a page of the original Exchange Server modules - place all configuration variables in separate scripts, which are in turn dot-sourced when initializing a new session:
Properties file:
$types = "*.txt"
$in = Join-Path $env:USERPROFILE Downloads
Script:
# dot source the variables
. (Join-Path $PSScriptRoot properties.ps1)
# do the actual work
Get-ChildItem $in -Include $types

Related

Function from imported script doesn't execute

i write a script with a function.
here is the script with the function:
function GenerateHashesForProjects(){
[array]$result=#()
Write-Output "Genrate Hash Values"
$dependencyFolder = Join-Path -Path $PSScriptRoot -ChildPath "..\..\Sources\_Dependencies"
#get all folder in a list below the dependency folder expect the "Modules" folder
$dependencyContent = Get-ChildItem -Path $dependencyFolder | where {$_.PSIsContainer -and ($_.Name -notlike "*Modules*")}
#Fill the result array with the project file name and the depending hash value of this file
foreach ($item in $dependencyContent) {
$denpencyProjects = Get-ChildItem -Path $item.Fullname | where { ($_ -like "*.csproj") }
$hashValue = (Get-FileHash $denpencyProjects.FullName -Algorithm MD5).Hash
$name = $denpencyProjects.Name
Write-Output "name: $name `nvalue: $hashValue"
$result += #($denpencyProjects.Name, $hashValue)
}
return $result
}
That script works fine.
Now i want to use this function also in another script. So i import the script and define a variable with that function. Here is the issue if a call the function without the variable it works fine but with the variable definition not, why?
Here is the second script with the import:
. Join-Path -Path $PSScriptroot -ChildPath "..\..\Build\Tools\GenerateHashesForProjects.ps1"
[array]$dependencyFileValues = GenerateHashesForProjects
This test works fine:
. Join-Path -Path $PSScriptroot -ChildPath "..\..\Build\Tools\GenerateHashesForProjects.ps1"
GenerateHashesForProjects
since you didn't post any responses to questions [grin], here is one way to rewrite your code.
what it does ...
creates an advanced function
uses the recommended name format for such
does not supply the "otta be there" Comment Based Help [grin]
defines the parameters
only the $Path is required.
defines but does not use a begin {} block
defines a process {} block
grabs a list of the dirs that branch from the source path
filters out the dirs that are in the $ExcludeDirList
gets the files in those dirs that match the $FileFilter
iterates thru that list
builds a [PSCustomObject] for each file with the desired details
you can add or remove them as needed.
sends that PSCO out to the calling code
the line that calls the function stores the entire set of results into the $Result variable and then shows that on screen.
a few notes ...
i had to change a lot of your details since i have no csproj files
there are no "what is happening" lines
if you need that, you can easily add such. i would NOT use Write-Output, tho, since that will pollute your output data.
there is no error detection OR error handling
here's the code ...
function Get-ProjectFileHash
{
<#
CommentBasedHelp goes here
#>
[CmdletBinding ()]
Param
(
[Parameter (
Mandatory,
Position = 0
)]
[string]
$Path,
[Parameter (
Position = 1
)]
[ValidateSet (
'MD5',
'MACTripleDES',
'RIPEMD160',
'SHA1',
'SHA256',
'SHA384',
'SHA512'
)]
[string]
$Algorithm = 'MD5',
[Parameter (
Position = 2
)]
[string[]]
$ExcludeDirList,
[Parameter (
Position = 3
)]
[string]
$FileFilter
)
begin {}
process
{
$ProjDirList = Get-ChildItem -LiteralPath $Path -Directory |
Where-Object {
# the "-Exclude" parameter of G-CI is wildly unreliable
# this avoids that problem [*grin*]
# build a regex OR listing to exclude
$_.Name -notmatch ($ExcludeDirList -join '|')
}
$FileList = Get-ChildItem -LiteralPath $ProjDirList.FullName -File -Filter $FileFilter
foreach ($FL_Item in $FileList)
{
[PSCustomObject]#{
FileName = $FL_Item.Name
DirName = $FL_Item.Directory
Algorithm = $Algorithm
Hash = (Get-FileHash -LiteralPath $FL_Item.FullName -Algorithm $Algorithm).Hash
}
}
}
end {}
} # end >>> function Get-ProjectFileHash
$Source = 'C:\ProgramData\chocolatey\lib'
$NotWanted = 'choco', '7zip', 'kb', 'bad', 'bkp'
$Filter = '*.nupkg'
$Result = Get-ProjectFileHash -Path $Source -Algorithm MD5 -ExcludeDirList $NotWanted -FileFilter $Wanted
$Result
truncated output ...
FileName DirName Algorithm Hash
-------- ------- --------- ----
autohotkey.nupkg C:\ProgramData\chocolatey\lib\autohotkey MD5 35A1B894AEA7D3473F3BBCBF5788D2D6
autohotkey.install.nupkg C:\ProgramData\chocolatey\lib\autohotkey.install MD5 EFE8AD812CBF647CFA116513AAD4CC15
autohotkey.portable.nupkg C:\ProgramData\chocolatey\lib\autohotkey.portable MD5 D31FA1B5496AAE266E4B0545835E9B19
[*...snip...*]
vcredist2015.nupkg C:\ProgramData\chocolatey\lib\vcredist2015 MD5 56321731BC0AEFCA3EE5E547A7A25D5E
vlc.nupkg C:\ProgramData\chocolatey\lib\vlc MD5 8177E24675461BDFF33639BF1D89784B
wiztree.nupkg

Too many if else statements

I am trying to check if a string "03-22-2019" exists (or not and show the result in output) the file is of ".sql" or ".txt" .
execution\development\process.sql
insert.sql
production\logs.txt
restore.sql
rebuild.txt
I am trying below code but I did with too many if else. The above file path stored in the $paths variable. I need to split the path with "\" and get the last part of the path to do something else.
if ($paths -like "*.sql") {
if ($paths.Contains("\")) {
$last = $paths.Split("\")[-1] #get the last part of the path
$result = "03-22-2019" #get this date from some where else
if ($result) {
#check if pattern string exists in that file.
$SEL = Select-String -Path <path location> -Pattern $result
if ($SEL -ne $null) {
Write-Host "`n $last Contains Matching date "
} else {
Write-Host "`n $last Does not Contains date"
}
} else {
Write-Host "`ndate field is blank"
}
} else {
$result = "03-22-2019" #get this date from some where else
if ($result) {
#check if pattern string exists in that file.
$SEL = Select-String -Path <path location> -Pattern $result
if ($SEL -ne $null) {
Write-Host "`n $last Contains Matching date "
} else {
Write-Host "`n $last Does not Contains date"
}
} else {
Write-Host "`ndate field is blank"
}
}
} elseIf ($paths -like "*.txt") {
if ($paths.Contains("\")) {
$last = $paths.Split("\")[-1] #get the last part of the path
$result = "03-22-2019" #get this date from some where else
if ($result) {
#check if pattern string exists in that file.
$SEL = Select-String -Path <path location> -Pattern $result
if ($SEL -ne $null) {
Write-Host "`n $last Contains Matching date "
} else {
Write-Host "`n $last Does not Contains date"
}
} else {
Write-Host "`ndate field is blank"
}
} else {
$result = "03-22-2019" #get this date from some where else
if ($result) {
#check if pattern string exists in that file.
$SEL = Select-String -Path <path location> -Pattern $result
if ($SEL -ne $null) {
Write-Host "`n $last Contains Matching date "
} else {
Write-Host "`n $last Does not Contains date"
}
} else {
Write-Host "`ndate field is blank"
}
}
} else {
Write-Host "other file types"
}
I would make this a little simpler, below is an example to determine if a file contains the date:
$paths = #("c:\path1","c:\path2\subdir")
ForEach ($path in $paths) {
$files = Get-ChildItem -LiteralPath $path -file -include "*.sql","*.txt"
$last = ($path -split "\\")[-1] # contains the last part of the path
$output = ForEach ($file in $files) {
If (Select-String -path $file -pattern "03-22-2019") {
"$($file.fullname) contains the date."
}
else {
"$($file.fullname) does not contain the date."
}
}
}
$output # outputs whether or not a file has the date string
The outer ForEach loop loops through the paths in $paths. Inside of that loop, you can do what you need to each path $path. I used $last to store the last part of the path in the current iteration. You have not said what to do with that.
The inner ForEach checks each .txt and .sql file for the date text 03-22-2019. $output stores a string indicating whether each .txt and .sql file contains the date string.
If your paths contain the file names, then you can use the following alternatives to grab the file name (last part of the path):
$path | split-path -leaf # inside of the outer ForEach loop
# Or
$file.name # inside of the inner ForEach loop
Looks like you should start with a foreach after your $paths variable like this:
foreach ($path in $paths) {
if ($path -like "*.sql") { #Note the use of one single item in the $paths array you have
$last = $path.split("\")[-1]
$result = #Whatever method you want to use to return a DateTime object
if ($result) { #### Now, this line doesn't make any sense. Do you want to compare a date to an older date or something? Maybe something like "if ($result -ge (Get-Date).addDays(-1) )
{ # Do your stuff }
Doing something like:
if ($paths -like "*.sql")
Doesn't work because $paths is an array and you are making a string comparison and never the two shall meet. Now, if you are trying to find if a string is inside a file, you should use something like "Get-Content" or "Import-Csv"
You can use the "Get-Date" cmdlet to get many different formats for the date. Read about that here. If you are trying to compare multiple dates against multiple files, I would start with a for loop on the files like I did up there, and then a for loop on each file for an array of dates. Maybe something like this:
foreach ($path in $paths) {
foreach ($date in $dates) {
# Get the contents of a file and store it in a variable
# Search for the string in that variable and store the results in a variable
# Write to the console
} # End foreach ($date in $dates)
} # End foreach ($path in $paths)
Post some more updated code and let's see what you come up with.

Test-Path - Variable Part of FileName - Variable doesn't get substituted

SourceFile.txt has the below
<somepath>\filename_$tminus1.csv
The below is the script I've written.
$date = [DateTime]::ParseExact("21-Aug-18", "dd-MMM-yy", $null)
if ($date.DayOfWeek -eq "Monday") {
$tminus1 = $date.AddDays(-3).ToString('yyyyMMdd')
$tminus2 = $date.AddDays(-4).ToString('yyyyMMdd')
Write-Host "Date vaulues set for Monday"
} else {
$tminus1 = $date.AddDays(-1).ToString('yyyyMMdd')
$tminus2 = $date.AddDays(-2).ToString('yyyyMMdd')
Write-Host "Date vaulues set for Non-Monday"
}
$files = Get-Content -Path $PSScriptRoot\SourceFile.txt
foreach ($file in $files) {
Convert-Path $file
if (-not (Test-Path $file)) {
echo ""
Write-Host "$file doesn't exist"
} else {
echo ""
Write-Host "$file exists"
}
}
The Test-Path cmdlet doesn't get the variable value substituted while looking for the file. It prints the below.
<somepath>\filename_$tminus1.csv doesn't exist
I expect the $tminus1 value gets substituted as 20180823 so that the else part prints the below.
<somepath>\filename_$tminus1.csv exists
The problem is when you pull the text in from your CSV it is treated as a literal, as if it as was a single quoted string. You can force PowerShell to re-evalueate the string like so:
$file = $ExecutionContext.InvokeCommand.ExpandString($file)
Or an alternative method would be to change the text in the CSV to this:
<somepath>\filename_{0}.csv
You can then use the format string to apply the variable like so:
$file = $file -f $tminus1
When you read strings from a file, PowerShell does not perform the normal interpolation it does for strings in your code. One way to get around this is to use Invoke-Expression.
Say you have a file (paths.txt) with these paths in it:
C:\Data\$dir1
C:\Data\$dir2
You can get the real paths like this:
$dir1 = "Directory1"
$dir2 = "Directory2"
Get-Content paths.txt |
ForEach-Object {
Invoke-Expression """$_"""
}
This will output:
C:\Data\Directory1
C:\Data\Directory2
Obviously, you can capture these to a variable, send them along the pipeline, etc, instead of just outputting them.

trying to specify the file path

I am trying to specify my file path in the script that I got from here: https://gallery.technet.microsoft.com/scriptcenter/Outputs-directory-size-964d07ff
The current file path points to the directory, but I am unable to locate the variable that I need to change in order to specify a different path.
# Get-DirStats.ps1
# Written by Bill Stewart (bstewart#iname.com)
# Outputs file system directory statistics.
#requires -version 2
<#
.SYNOPSIS
Outputs file system directory statistics.
.DESCRIPTION
Outputs file system directory statistics (number of files and the sum of all file sizes) for one or more directories.
.PARAMETER Path
Specifies a path to one or more file system directories. Wildcards are not permitted. The default path is the current directory (.).
.PARAMETER LiteralPath
Specifies a path to one or more file system directories. Unlike Path, the value of LiteralPath is used exactly as it is typed.
.PARAMETER Only
Outputs statistics for a directory but not any of its subdirectories.
.PARAMETER Every
Outputs statistics for every directory in the specified path instead of only the first level of directories.
.PARAMETER FormatNumbers
Formats numbers in the output object to include thousands separators.
.PARAMETER Total
Outputs a summary object after all other output that sums all statistics.
#>
[CmdletBinding(DefaultParameterSetName="Path")]
param(
[parameter(Position=0,Mandatory=$false,ParameterSetName="Path",ValueFromPipeline =$true)]
$Path=(get-location).Path,
[parameter(Position=0,Mandatory=$true,ParameterSetName="LiteralPath")]
[String[]] $LiteralPath,
[Switch] $Only,
[Switch] $Every,
[Switch] $FormatNumbers,
[Switch] $Total
)
begin {
$ParamSetName = $PSCmdlet.ParameterSetName
if ( $ParamSetName -eq "Path" ) {
$PipelineInput = ( -not $PSBoundParameters.ContainsKey("Path") ) -and ( -
not $Path )
}
elseif ( $ParamSetName -eq "LiteralPath" ) {
$PipelineInput = $false
}
# Script-level variables used with -Total.
[UInt64] $script:totalcount = 0
[UInt64] $script:totalbytes = 0
# Returns a [System.IO.DirectoryInfo] object if it exists.
function Get-Directory {
param( $item )
if ( $ParamSetName -eq "Path" ) {
if ( Test-Path -Path $item -PathType Container ) {
$item = Get-Item -Path $item -Force
}
}
elseif ( $ParamSetName -eq "LiteralPath" ) {
if ( Test-Path -LiteralPath $item -PathType Container ) {
$item = Get-Item -LiteralPath $item -Force
}
}
if ( $item -and ($item -is [System.IO.DirectoryInfo]) ) {
return $item
}
}
# Filter that outputs the custom object with formatted numbers.
function Format-Output {
process {
$_ | Select-Object Path,
#{Name="Files"; Expression={"{0:N0}" -f $_.Files}},
#{Name="Size"; Expression={"{0:N0}" -f $_.Size}}
}
}
# Outputs directory statistics for the specified directory. With -recurse,
# the function includes files in all subdirectories of the specified
# directory. With -format, numbers in the output objects are formatted with
# the Format-Output filter.
function Get-DirectoryStats {
param( $directory, $recurse, $format )
Write-Progress -Activity "Get-DirStats.ps1" -Status "Reading
'$($directory.FullName)'"
$files = $directory | Get-ChildItem -Force -Recurse:$recurse | Where-
Object
{ -not $_.PSIsContainer }
if ( $files ) {
Write-Progress -Activity "Get-DirStats.ps1" -Status "Calculating
'$($directory.FullName)'"
$output = $files | Measure-Object -Sum -Property Length | Select-Object
`
#{Name="Path"; Expression={$directory.FullName}},
#{Name="Files"; Expression={$_.Count; $script:totalcount += $_.Count}},
#{Name="Size"; Expression={$_.Sum; $script:totalbytes += $_.Sum}}
}
else {
$output = "" | Select-Object `
#{Name="Path"; Expression={$directory.FullName}},
#{Name="Files"; Expression={0}},
#{Name="Size"; Expression={0}}
}
if ( -not $format ) { $output } else { $output | Format-Output }
}
}
... the rest of the code did not seem relevant
You either specify the $Path variable when calling the script, or add a line that overrides the default value. I've highlighted where this is below.
[CmdletBinding(DefaultParameterSetName="Path")]
param(
[parameter(Position=0,Mandatory=$false,ParameterSetName="Path",ValueFromPipeline =$true)]
$Path=(get-location).Path, ################ PATH IS SET HERE ##############
[parameter(Position=0,Mandatory=$true,ParameterSetName="LiteralPath")]
[String[]] $LiteralPath,
[Switch] $Only,
When calling script:
C:>.\myscript.ps1 -Path "c:\temp"
what you call the value depends on where you call it from.
the "main" part of this cmdlet accepts one of a couple parameters; path and literalPath, path would be used in preference to literal path. If neither is specified the current working directory will be the starting point. passing different arguments to the cmdlet seems to be the easiest technique. The author's intended usage.
BUT...
Up in that first function, after the parameters are bound in the "begin" section... the actual path is "$item".
inside Get-DirectoryStats it's being referred to as $directory.
There are places where it's referred to as $_.
There are a lot of articles on the topic of "scope". here's one: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_scopes

Powershell - Overiding Arguments Relase definition

I am trying to create a Release definition in TFS 2017 with powershell script
I am runing a powershell script that has to find a path of a setParameters.xml file which it does and it suppose to over ride all the tokens but the it just ignores the loop and the arguments stay the same.
Script code:
$vars = Get-ChildItem -path env:*
# read in the setParameters file
$contents = Get-Content -Path $paramsFilePath
# perform a regex replacement
$newContents = "";
$contents | % {
$line = $_
if ($_ -match "__(\w+)__") {
$setting = Get-ChildItem -path env:* | ? { $_.Name -eq $Matches[1] }
if ($setting) {
Write-Verbose -Verbose ("Replacing key {0} with value from environment" -f $setting.Name)
$line = $_ -replace "__(\w+)__", $setting.Value
}
}
$newContents += $line + [Environment]::NewLine
}
Why is the loop being skiped and how could i fix it so it overwrites the arguments in SetParameters.xml file?
For those interested I have ditched this powershell and used Replace Token step in Release definition.
I think its a more efficient way to replace tokens