Saturday, October 5, 2019
Tuesday, October 1, 2019
Friday, July 12, 2019
Sunday, June 2, 2019
Monday, May 20, 2019
Friday, May 10, 2019
Sunday, May 5, 2019
Get List of Files and Insert to SQL Server Table
Get-ChildItem -Path C:\ETL\EXPORT_FILES |
Select-Object Name, FullName, Extension, CreationTime, Length |
Write-SqlTableData -ServerInstance localhost -DatabaseName ETL -SchemaName import -TableName ImportFileList -Force
Initially loclhost was working but then it stopped with failed to connect message; changed to actual SQL Server instance name
and that worked:
Initially loclhost was working but then it stopped with failed to connect message; changed to actual SQL Server instance name
and that worked:
Get-ChildItem -Path C:\ETL\EXPORT_FILES |
Select-Object Name, FullName, Extension, CreationTime, Length |
Write-SqlTableData -ServerInstance "DESKTOP-U8PAEJC" -DatabaseName "DEV_STAGING" -SchemaName "import" -TableName "ImportFileList" -Force
Get List of Files in Folder and Pipe to Excel
Get-ChildItem -Path C:\ETL\EXPORT_FILES |
Select-Object Name, FullName, Extension, CreationTime, Length |
Export-Excel -Now
Import Excel to SQL Server Table
Import-Excel -Path C:\ETL\EXCEL-FILES\CONTACT.xlsx |
Write-SqlTableData -ServerInstance localhost -DatabaseName ETL -SchemaName import -TableName Contact -Force
-Force on Write-SqlTableData will create anything that doesn't exist; e.g. Databasename, SchemaName, TableName
Saturday, May 4, 2019
Thursday, May 2, 2019
Strings in PowerShell
https://mcpmag.com/articles/2015/07/23/strings-in-powershell.aspx
Note "HERE" string
@"
string content goes here; can be multiple line; can include variables; e.g. $varname
"@
Another way of evaluating a string within a string"
Note "HERE" string
@"
string content goes here; can be multiple line; can include variables; e.g. $varname
"@
Another way of evaluating a string within a string"
$MyName = "State Your Name" "Hello $($MyName), today is $((Get-Date).DayOfWeek)"
Monday, April 29, 2019
Saturday, April 27, 2019
Wednesday, April 24, 2019
PowerShell 6
https://www.thomasmaurer.ch/2019/03/how-to-install-and-update-powershell-6/
C:\Program Files\PowerShell\6\pwsh
C:\Program Files\PowerShell\6\pwsh
Wednesday, April 17, 2019
Monday, April 15, 2019
Saturday, April 13, 2019
Where is PowerShell.exe
Open command prompt
where PowerShell
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
where PowerShell
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Query the PowerShell Version You're Running
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.590
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.590
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 590
$PSVersionTable.PSVersion.Major
5
Name Value
---- -----
PSVersion 5.1.17134.590
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.590
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 590
$PSVersionTable.PSVersion.Major
5
Thursday, April 11, 2019
Tuesday, April 2, 2019
Thursday, March 28, 2019
Monday, March 18, 2019
Create Pipe-Delimited File from Fixed-Width File
<# SPECIFY INPUT FILE NAME #>
$file = "FULL_PATH_TO_FIXED_WIDTH_FILE"
<# SPECIFY INPUT FILE NAME #>
$outputFileName = "FULL_PATH_TO_PIPE_DELIMITED_FILE"
$count = 0
$instream = New-Object System.IO.StreamReader($file)
$outstream = New-Object System.IO.StreamWriter($outputFileName, 0) <# create new file #>
<# write out columns names in the first row #>
$outstream.WriteLine("<< PIPE DELIMITED LIST OF FIELD NAMES FOR 1ST ROW >>")
while (($line = $instream.ReadLine()) -ne $null)
{
$SYS_ID = $line.Substring(30, 1)
if ($SYS_ID -eq "K" -or $SYS_ID -eq "L") {
<# add $line.Substring(OFFSET, LENGTH) for each field to extract #>
$out = $line.Substring(0, 44) + "|" + `
$line.Substring(0, 2) + "|" + `
$line.Substring(2, 2) + "|" + `
$line.Substring(4, 2) + "|" + `
$line.Substring(6, 8) + "|" + `
$line.Substring(14, 9) + "|" + `
$line.Substring(23, 1) + "|" + `
$line.Substring(24, 6) + "|" + `
$line.Substring(30, 1) + "|" + `
$line.Substring(31, 9) + "|" + `
$line.Substring(40, 2) + "|" + `
$line.Substring(44, 5) + "|" + `
$line.Substring(89, 8) + "|" + `
$line.Substring(102, 10) + "|" + `
$line.Substring(112, 10) + "|" + `
$line.Substring(122, 10) + "|" + `
$line.Substring(132, 2) + "|" + `
$line.Substring(174, 12) + "|" + `
$line.Substring(195, 4) + "|" + `
$line.Substring(228, 4) + "|" + `
$line.Substring(228, 4) + "|" + `
$line.Substring(296, 10) + "|" + `
$line.Substring(436, 9) + "|" + `
$line.Substring(524, 4) + "|" + `
$line.Substring(528, 8) + "|" + `
$line.Substring(528, 8) + "|" + `
$line.Substring(536, 8) + "|" + `
$line.Substring(544, 8) + "|" + `
$line.Substring(560, 8) + "|" + `
$line.Substring(568, 8) + "|" + `
$line.Substring(585, 69) + "|" + `
$line.Substring(710, 1) + "|" + `
$line.Substring(891, 4) + "|" + `
$line.Substring(953, 8) + "|" + `
$line.Substring(961, 1) + "|" + `
$line.Substring(978, 40) + "|" + `
$line.Substring(1078, 11) + "|" + `
$line.Substring(1089, 11) + "|" + `
$line.Substring(2865, 2) + "|" + `
$line.Substring(2867, 8) + "|" + `
$line.Substring(2875, 8)
$outStream.WriteLine($out)
$count = $count + 1
}
}
$instream.Close();
$outstream.Close();
$file = "FULL_PATH_TO_FIXED_WIDTH_FILE"
<# SPECIFY INPUT FILE NAME #>
$outputFileName = "FULL_PATH_TO_PIPE_DELIMITED_FILE"
$count = 0
$instream = New-Object System.IO.StreamReader($file)
$outstream = New-Object System.IO.StreamWriter($outputFileName, 0) <# create new file #>
<# write out columns names in the first row #>
$outstream.WriteLine("<< PIPE DELIMITED LIST OF FIELD NAMES FOR 1ST ROW >>")
while (($line = $instream.ReadLine()) -ne $null)
{
$SYS_ID = $line.Substring(30, 1)
if ($SYS_ID -eq "K" -or $SYS_ID -eq "L") {
<# add $line.Substring(OFFSET, LENGTH) for each field to extract #>
$out = $line.Substring(0, 44) + "|" + `
$line.Substring(0, 2) + "|" + `
$line.Substring(2, 2) + "|" + `
$line.Substring(4, 2) + "|" + `
$line.Substring(6, 8) + "|" + `
$line.Substring(14, 9) + "|" + `
$line.Substring(23, 1) + "|" + `
$line.Substring(24, 6) + "|" + `
$line.Substring(30, 1) + "|" + `
$line.Substring(31, 9) + "|" + `
$line.Substring(40, 2) + "|" + `
$line.Substring(44, 5) + "|" + `
$line.Substring(89, 8) + "|" + `
$line.Substring(102, 10) + "|" + `
$line.Substring(112, 10) + "|" + `
$line.Substring(122, 10) + "|" + `
$line.Substring(132, 2) + "|" + `
$line.Substring(174, 12) + "|" + `
$line.Substring(195, 4) + "|" + `
$line.Substring(228, 4) + "|" + `
$line.Substring(228, 4) + "|" + `
$line.Substring(296, 10) + "|" + `
$line.Substring(436, 9) + "|" + `
$line.Substring(524, 4) + "|" + `
$line.Substring(528, 8) + "|" + `
$line.Substring(528, 8) + "|" + `
$line.Substring(536, 8) + "|" + `
$line.Substring(544, 8) + "|" + `
$line.Substring(560, 8) + "|" + `
$line.Substring(568, 8) + "|" + `
$line.Substring(585, 69) + "|" + `
$line.Substring(710, 1) + "|" + `
$line.Substring(891, 4) + "|" + `
$line.Substring(953, 8) + "|" + `
$line.Substring(961, 1) + "|" + `
$line.Substring(978, 40) + "|" + `
$line.Substring(1078, 11) + "|" + `
$line.Substring(1089, 11) + "|" + `
$line.Substring(2865, 2) + "|" + `
$line.Substring(2867, 8) + "|" + `
$line.Substring(2875, 8)
$outStream.WriteLine($out)
$count = $count + 1
}
}
$instream.Close();
$outstream.Close();
How to Combine Multiple Text Files into a Single File
<# Combine multiple claims files into a single file #>
$combinedClaimsFiles = "TARGET_PATH_FILENAME_GOES_HERE"
$inputFileList = "INPUT_FILES_GO_HERE_EG_*.TXT"
New-Item -ItemType file $combinedClaimsFiles –force
Get-Content $inputFileList | Add-Content $combinedClaimsFiles
$combinedClaimsFiles = "TARGET_PATH_FILENAME_GOES_HERE"
$inputFileList = "INPUT_FILES_GO_HERE_EG_*.TXT"
New-Item -ItemType file $combinedClaimsFiles –force
Get-Content $inputFileList | Add-Content $combinedClaimsFiles
Thursday, February 28, 2019
Wednesday, February 27, 2019
Read a File 1 Line at a Time
$file = "FILE NAME GOES HERE"
$line = Get-Content $file | Select -First 1
$line.Substring(0, 50)
$line.Length
<# Get-Content $file | Select -First 10 | ForEach-Object { $_ } #>
$count = 0
$stream = New-Object System.IO.StreamReader($file)
Write-Output("CL_PA_ID|CL_SYS_ID")
while (($line = $stream.ReadLine()) -ne $null -and $count -lt 100)
{
$CL_SYS_ID = $line.Substring(30, 1)
if ($CL_SYS_ID -eq "K" -or $CL_SYS_ID -eq "L") {
$out = $line.Substring(14, 9) + "|" + $line.Substring(30, 1)
Write-Output($out)
$count = $count + 1
}
}
$stream.Close();
$line = Get-Content $file | Select -First 1
$line.Substring(0, 50)
$line.Length
<# Get-Content $file | Select -First 10 | ForEach-Object { $_ } #>
$count = 0
$stream = New-Object System.IO.StreamReader($file)
Write-Output("CL_PA_ID|CL_SYS_ID")
while (($line = $stream.ReadLine()) -ne $null -and $count -lt 100)
{
$CL_SYS_ID = $line.Substring(30, 1)
if ($CL_SYS_ID -eq "K" -or $CL_SYS_ID -eq "L") {
$out = $line.Substring(14, 9) + "|" + $line.Substring(30, 1)
Write-Output($out)
$count = $count + 1
}
}
$stream.Close();
Subscribe to:
Comments (Atom)