I'm trying to generate an image based on a 6 char hex code (color code) that's being generated in powershell, but I'm getting an error for System.String when I add the non-digit characters, script is as follows:
# Generate random hex code for the background color
$backgroundColor = "#" + [System.String]::Join("", (Get-Random -Count 6 -InputObject (0..9 + "a".."f") | ForEach-Object { $_ }))
# Generate random hex code for the text color
$textColor = "#" + [System.String]::Join("", (Get-Random -Count 6 -InputObject (0..9 + "a".."f") | ForEach-Object { $_ }))
The error I'm receiving is this:
Cannot convert value "a" to type "System.Int32". Error: "Input string was not in a correct format."
At line:5 char:1
+ $backgroundColor = "#" + [System.String]::Join("", (Get-Random -Count ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
When I remove the "a".."f" portion, it generates a 6 digit hex just fine, but I need to have the alphas as well for the full spectrum, not sure how to include those characters in these snippits.
Any advice would be greatly appreciated.
I've really only tried removing the alphas to test the rest of the script which works fine, but only generates a code with the integers 0-9 which works, but isn't desired.
Use [char[]]"0123456789ABCDEF" instead of 0..9 + "a".."f".
$backgroundColor = "#" + [System.String]::Join("", (Get-Random -Count 6 -InputObject ([char[]]"0123456789ABCDEF") | ForEach-Object { $_ }))
$textColor = "#" + [System.String]::Join("", (Get-Random -Count 6 -InputObject ([char[]]"0123456789ABCDEF") | ForEach-Object { $_ }))
UPDATE: Shorter version.
$backgroundColor = '#'+ (([char[]]"0123456789ABCDEF" | Get-Random -Count 6) -join '')
$textColor = '#'+ (([char[]]"0123456789ABCDEF" | Get-Random -Count 6) -join '')
Your code as is will succeed in PowerShell Core, however isn't compatible with Windows PowerShell. Main reason is, the range operator .. on characters (i.e.: 'a'..'f') became a thing in PowerShell v6:
To overcome this you can change your logic a bit so it is compatible with both versions:
$map = [char] '0'..[char] '9' + [char] 'a'..[char] 'f'
'#' + [string]::new((Get-Random -Count 6 -InputObject $map))
To work in powershell 5.1 with the [char[]] cast. I don't think you need the foreach-object.
[int][char]'a'
97
[int][char]'f'
102
"#" + [System.String]::Join("", (Get-Random -Count 6 -InputObject (0..9 +
[char[]](97..102))))
#d1b5a8
Or
"#" + -join (0..9 + [char[]]([char]'a'..[char]'f') | Get-Random -Count 6)
I have a PowerShell script where I use the Call operator to run mkvmerge.exe with a set of arguments/options. I wanted to collect the options in a string so that the string could be expanded based on inputs earlier in the script, but when the destination file is in this string mkvmerge does not recognize it for some reason. I am unsure if this is because of variable expansion or what the issue is
The code below illustrates the problem. I have also tried adding the options to an array and using the # operator, but with no luck
$mkvmerge = "C:\Program Files\MKVToolNix\mkvmerge.exe"
$applyoptions = $true
$subtitleformat = "srt"
$isolanguage = "eng"
foreach ($file in Get-ChildItem -File -Include *.mkv) {
<# Make the string with options for mkvmerge #>
$mkvmergeparams = ""
if ($applyoptions -eq $true) {
$mkvmergeinstructions += ('`' + "#options.json")
}
$mkvmergeparams += ("-o " + "`"" + $file.DirectoryName + "\Remux-" + $file.BaseName + ".mkv`" " + "`"" + $file + "`"")
if (-not($subtitleformat -eq "")) {
$mkvmergeinstructions += (" --default-track " + $subtitledefault) + " --language " + "0:" + $isolanguage + " " + "`"" + $file.DirectoryName + "\" + $file.BaseName + "." + $subtitleformat + "`""
}
<# Check the string #>
Write-Host $mkvmergeparams
<# This does not work, but I would like it to #>
& $mkvmerge $mkvmergeparams
<# This works, but would require a separate line for each set of possible options #>
#& $mkvmerge `#options.json -o ($file.DirectoryName + "\" + "Remux-" + $file.BaseName + ".mkv") $file --default-track $subtitledefault --language ("0:" + $isolanguage) ($file.DirectoryName + "\" + $file.BaseName + "." + $subtitleformat)
}
Read-Host -Prompt "Press enter to exit"
In the second example the variables are expanded and mkvmerge recognizes the options given, but this requires having a separate mkvmerge line for each set of possible inputs
When checking the string it seems to expand to look exactly as it should
Found the answer. I had to create an array with the arguments
<# Build array with options for mkvmerge #>
$mkvmergeparams = $null
if ($applyoptions -eq $true) {$mkvmergeparams += #('#options.json')}
$mkvmergeparams += #("-o", ($file.DirectoryName + "\Remux-" + $file.BaseName + ".mkv"), $file)
if (-not($subtitleformat -eq "")) {$mkvmergeparams += #("--default-track", $subtitledefault, "--language", ("0:" + $isolanguage), ($file.DirectoryName + "\" + $file.BaseName + "." + $subtitleformat))}
I'm trying to loop through a series of txt files extracting information into arrays based a : delimiter.
All values i'm taking from the text files fit on a single line, except for one. (The "ad text" value). This cuts off the information after line 1 in the final output. When I remove line carriages and breaks beforehand, none of the fields are inputted correctly.
How would I specify wanting my array to accept multi-line inputs for the "ad text" field?
Below is the code i'm working with:
$files = ls "*.txt"
$dictionary = #{}
[System.Collections.Generic.List[String]]$list = #()
foreach($f in $files){$in = Get-Content $f
$in.Split([Environment]::NewLine) | ForEach-Object { $key,$value = $_.Split(':')
$dictionary[$key] = $value
}
[void]$list.Add( $dictionary['Ad ID'] + ',' + $dictionary['Ad Text'] + ',' + $dictionary['Ad Landing Page'] + ',' + $dictionary['Ad Targeting Location'] + ',' + $dictionary['Age'] + ',' + $dictionary['Language'] + ',' + $dictionary['Placements'] + ',' + $dictionary['Interests'] + ',' + $dictionary['Behaviors'] + ',' + $dictionary['Ad Impressions'] + ',' + $dictionary['Ad Clicks'] + ',' + $dictionary['Ad Spend'] + ',' + $dictionary['Ad Creation Date'] + ','+ $dictionary['Friends'] + ',' + $dictionary['Ad End Date'] + ',' + $dictionary['Excluded Connections'] + ',' + $dictionary['Image'] + ',' + $dictionary['Ad Targeting Location']+','+$dictionary[‘File Name’] )
}
$list | Out-File -FilePath '.\trial.csv' -Append
Assuming that the additional lines following Ad Text:-prefixed lines do not contain : chars themselves:
# Create sample text file t.txt
#'
Ad ID:10
Ad Text:one
two
Ad Landing Page:http://example.org
'# > t.txt
# Split the lines into field names and values by ":" and
# build a dictionary.
$dict = [ordered] #{}
$i = 0
(Get-Content -Raw t.txt) -split '(?m)^([^\n:]+):' -ne '' | ForEach-Object {
if ($i++ % 2 -eq 0) {
$key = $_
} else {
$dict[$key] = $_
}
}
# Output the dictionary, showing each entry as a list.
$dict | Format-List
The output is as follows, showing that the Ad Text entry comprises two lines:
Name : Ad ID
Value : 10
Name : Ad Landing Page
Value : http://example.org
Name : Ad Text
Value : one
two
I writing a script in Powershell to gather information about how many files are in my archive folder, the script is to count the files in the folder then it deletes file older than give date and then it counts the number of files again to report how many files it deleted.
I can get this to work however I am running into a little problem with the syntax. When I have it like this it works fine.
Clear-Host
$ErrorFiles = "c:\data\ErrorFiles"
$Archivefiles = "c:\data\Archivefiles"
$PreCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PreCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
Get-ChildItem $ErrorFiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
Get-ChildItem $Archivefiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
$PostCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PostCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n"
$str2 = "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
write-host $str1
write-host $str2
However if I try to change the #str1 assignment to have it in one line and take both the strings I get this error (see code below)
Cannot convert value "Number of files in" to type "System.Int32". Error: "Input string was not in a correct format."
At line:13 char:1
+ $str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
Clear-Host
$ErrorFiles = "c:\data\ErrorFiles"
$Archivefiles = "c:\data\Archivefiles"
$PreCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PreCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
Get-ChildItem $ErrorFiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
Get-ChildItem $Archivefiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
$PostCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PostCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n" +
- "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
write-host $str1
Thanks for you help!
Remove the hyphen from the beginning of the second line:
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n" +
- "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
^
In genereal, the format operator (-f) is a better approach to building strings like this:
$str1 = "Number of files in {0} was {1} number of files deleted {2} and {3} " +
"are left.`nNumber of files in {4} was {5} number of files deleted " +
"{6} and {7} are left.`n"
$str1 -f $ErrorFiles, $PreCountErrorFiles,
($PreCountErrorFiles - $PostCountErrorFiles),
$PostCountErrorFiles, $Archivefiles, $PreCountArchivefiles,
($PreCountArchivefiles - $PostCountArchivefiles),
$PostCountArchivefiles
Hello I am trying to figure out how to set up an SQL Server Agent Process that will pick up a file that was created by the step and send an email with the attachment. The first step is below. Would I execute the send mail right after or another step? If so how do I would I go about it. Currently I just pick up the file and email it but we have multiple clients now / they want the file more often so I need to automate it.
Any direction or help on this would be great.
Declare #PeriodStart Datetime
Declare #PeriodEnd Datetime
SELECT #PeriodEnd = GetDate()
,#PeriodStart = dateadd(hour,-168,getdate())
;WITH outpQ
AS
(
SELECT 1 AS grpOrd, CAST(null AS VARCHAR(255)) AS posInGrp, 'A' AS Ord, CaseNumberKey, 'A|' + ClientsKey + '|' + CAST(BRTNumber AS VARCHAR(11)) + '|' + IsNull(replace(convert(char(10), CoverDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(char(10), CoverDate, 101), '/', ''), '') + '|' + IsNull(ParcelNumber, '') + '|' + IsNull(AssessedBeg, '') + '|' + IsNull(AssessedDim, '') + '|' + IsNull(AbbrLegal, '') + '|' + IsNull(WaterFrom, '') + '|' + IsNull(WaterTo, '') + '|' + IsNull(Cast(WaterOpen AS VARCHAR(50)), '') + '|' + IsNull(TaxFrom, '') + '|' + IsNull(TaxTo, '') + '|' + IsNull(Cast(TaxOpen AS VARCHAR(50)), '') AS Extract
FROM newCityCollection.dbo.PropertyInformation
WHERE DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 1 As grpOrd, null AS posInGrp, 'A1', A.CaseNumberKey, 'A1|' + '|' + '|' + B.LienNumber + '|' + IsNull(Cast(B.LienAmt AS VARCHAR(50)), '') + '|' + IsNull(replace(LienDate, '/', ''), '') + '|' + IsNull(B.LienReason, '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN newCityCollection.dbo.muniLiens B ON B.CaseNumberKey = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B', A.CaseNumberKey, 'B|' + '|' + IsNull(C.First, '') + '|' + IsNull(C.Middle, '') + '|' + IsNull(C.Last, '') + '|' + IsNull(C.Alias, '') + '|' + IsNull(C.ComName, '') + '|' + IsNull(C.DocRel, '') + '|' + Cast(C.InterestsKey AS VARCHAR(11)) AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN newCityCollection.dbo.Interests C ON C.CaseNumberKey = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B1', A.CaseNumberKey, 'B1|' + IsNull(FullAdd, '') + '|' + IsNull(D.City, '') + '|' + IsNull(D.State, '') + '|' + IsNull(D.Zip, '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN newCityCollection.dbo.Interests C ON C.CaseNumberKey = A.CaseNumberKey
JOIN newCityCollection.dbo.InterestAdd D ON D.CaseNumberKey = A.CaseNumberKey AND D.InterestsKey = C.InterestsKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B2', A.CaseNumberKey, 'B2|' + '|' + IsNull(E.SuitNumber, '') + '|' + Cast(E.BDate AS VARCHAR(11)) + '|' + IsNull(E.Chapter, '') + '|' + IsNull(E.VS, '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN newCityCollection.dbo.Interests C ON C.CaseNumberKey = A.CaseNumberKey
JOIN newCityCollection.dbo.Banks E ON E.CaseNumberKey = A.CaseNumberKey AND E.InterestsKey = C.InterestsKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 3 As grpOrd3, null AS posInGrp, 'B3', A.CaseNumberKey, 'B3|' + '|' + F.DocType + '|' + IsNull(Cast(F.DocAmt AS VARCHAR(50)), '') + '|' + IsNull(replace(convert(char(10), DocDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(char(10), RecDate, 101), '/', ''), '') + '|' + IsNull(F.DocID, '') + '|' + IsNull(F.Grantee,'') + '|' + IsNull(F.Grantor,'') + Cast(F.DocIDKey AS VARCHAR(11)) AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN newCityCollection.dbo.Documents F ON F.CaseNumberKey = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2
UNION ALL
SELECT 4 AS grpOrd, null AS posInGrp, 'C', A.CaseNumberKey, 'C|' + IsNull(J.CType, '') + '|' + IsNull(J.plaintiffName,'') + '|' + IsNull(J.plaintiffAdd1, '') + '|' + IsNull(J.plaintiffCity, '') + '|' + IsNull(J.plaintiffState, '') + '|' + IsNull(J.plaintiffZip, '') + '|' + '|' + IsNull(J.defendantName, '') + '|' + IsNull(J.defendantAdd1, '') + '|' + IsNull(J.defCity, '') + '|' + IsNull(J.defState, '') + '|' + IsNull(J.defZip, '') + '|' + '|' + IsNull(J.Court, '') + '|' + IsNull(J.CaseID, '') + '|' + IsNull(J.JAmt, '') + '|' + IsNull(replace(convert(VarChar(10), JDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(VARCHAR(10), revivedDate, 101), '/', ''), '') AS Extract
FROM newCityCollection.dbo.PropertyInformation A
JOIN Acme.new_judgment_system.dbo.selected_compiled_clean J ON J.CaseNumber = A.CaseNumberKey
WHERE A.DateFinished BETWEEN #PeriodStart AND #PeriodEnd AND ClientKey = 2 AND J.plaintiffName NOT IN (SELECT Plaintiff FROM newCityCollection.dbo.excluded_Plaintiffs)
)
--Extract data set into a table -- dump table in .txt with current date as part of name then delete that table
SELECT Extract INTO datadump FROM outpQ ORDER BY CaseNumberKey, grpOrd, posInGrp, Ord
DECLARE #FileName varchar(50),
#bcpCommand varchar(2000)
SET #FileName = REPLACE('D:\Argosy_import_'+CONVERT(char(8),GETDATE(),1)+'_0001.txt','/','')
SET #bcpCommand = 'bcp "SELECT Extract FROM datadump" QUERYOUT "'
SET #bcpCommand = #bcpCommand + #FileName + '" -U ******* -P *********** -T -c'
EXEC master..xp_cmdshell #bcpCommand
DROP table datadump
I added this after Drop Table Dump and it seems to work.
EXEC msdb.dbo.sp_send_dbmail
#recipients=N'******',
#body='Message Body',
#subject ='Import file for #PeriodStart',
#profile_name ='Mail',
#file_attachments ='D:Argosy_import_051713_0001.txt';