Powershell script to download either zip or csv - powershell

I use ssis to run a powershell script to download a file that used to be csv but recently became large enough to be zipped. I updated the powershell script to look for a zip file and added a task to the package to unzip the file so it can be loaded into a sql database. Well, then it came through as a csv again. I need a solution to choose either the zip file or the csv file. Not sure if this should be a task in ssis or updated powershell.

I would go with a PS task to download the files (either zip/csv) then SSIS foreach container to iterate over the files you just downloaded. Doing this you will assign the individual file to a user variable. Inside your container if the file is zip, (use a variable set via an expression to determine if it is zip or not) run a task that will run PS to unzip and then a expression task to update the variable that holds the file path to be the newly unzipped csv path. Then run your data flow task to import the csv.
If the file is a csv to begin with, then just run the DFT.
Either way the data flow task is the same, take csv and load it. I have found I like to keep my PS in SSIS packages very purpose driven. I have a tendency to build my logic in PS because it is easier, but then my package becomes harder to debug because an issue in my PS script will fail the SSIS package and SSIS tells me nothing usefull about what in the script failed. (unless you are handling redirecting of stdout and stderr from your PS, or doing some other logging)
Best to keep the powershell as simple as needed for each task you need to do.

Related

Using Microsoft.Data.SqlClient in PowerShell

I would like to use the Microsoft.Data.SqlClient namespace/ objects in a PowerShell script.
Two (click1, click2) Github posts provide a way to load the correct dll's, but the solutions don't seem to work anymore.
E.g. result of the first solution while copying the next files:
(Packages copied from .nuget/packages folder)
Microsoft.Data.SqlClient.dll
Microsoft.Data.SqlClient.SNI.x64.dll
Microsoft.Identity.Client.dll
Result: Could not load file or assembly 'System.Runtime, Version=6.0.0.0'
In addition, I've tried to create a Dummy Console App -> Added the Microsoft.Data.SqlClient Nuget package -> built the project and copied all dll's to the same folder as the PS script.
As soon as I start the script (using the 'Add-Type -Path' construction), it results in errors, such as 'couldn't load file or assembly - wrong version...' (this is strange, because the folder contains all dll's...)
Could you provide an alternative solution/ steps in order to use the described package in a PS script?

Change Directory to Folder Containing PowerShell Script - Regardless of Where That Folder Is Located

I have a script that I've created to prep our customer's servers for a software install. Part of this requires the script to be run as administrator, so just instructing people to click "Run With Powershell" doesn't get the job done. The script is in a folder with a number of .ini files that the script needs to copy to different server locations. If I just right-click the Powershell script and select "Run With Powershell," it is able to find the files and copy them without issue. Unfortunately, if I open the script in ISE, it opens with a default directory of C:\users\user, and I can't seem to copy those .ini files without first running a change directory command to get us to the folder that the script and the .ini files are in. But I'd like our installation techs to be able to run this without worrying about the exact location they initially drop these folders. I'd also like them to not have to worry about changing the directory manually in PowerShell. Some of our customers have multiple drives, and it might make sense to put this stuff on something other than the C drive, so it's hard to tell where this folder might end up. But I'm not sure of a command that will get me to the directory of the *.ps1 file, without knowing where that file is beforehand... Anyone have a suggestion?
You can use $PSScriptRoot that will have the location of the directory where the script is located.
This is referenced in the following post:
How can I get the file system location of a PowerShell script?

How do I get TFS 2013 Build definition to zip the build output?

I am using VS2013 and TFS2013. In TFS, I am using the new "default" TfvcTemplate12.xaml template.
In my build definition, in the Test-> Post-Build Script Arguments (or Path if I have to save the command in a file), what PowerShell script do I enter to get it to Zip the entire build output and drop that zip file onto a share drive? I want to avoid manipulating the XAML workflow.
I know the basic PS script to zip a folder is....
Get-Childitem C:\source -Recurse | Write-Zip -IncludeEmptyDirectories -OutputPath C:\stage\zomeZipFile.zip
But I don't know if this is the right approach, and if so, what to replace the source and stage parameters with.
Our code migration system requires us to have 1 ZIP file with everything in it for deployment.
You need to add the PowerShell to the post-test script location.
https://msdn.microsoft.com/en-us/library/hh850448.aspx
MSDN had a comprehensive list of the cracks that you have access to when it is executing.

Is it possible to run a batch file from package manager console?

I'm using code first migrations with my context class in a class library (ie not the startup project) and I want to make batch files for the common operations to save having to pass in the parameters each time I want to add-migration and update-database. I ran the "dir" command in the console and it appears to be in the solution root folder so I have tried creating a .bat,.cmd or .ps1 file in the Solution Items folder but the package manager powershell doesn't seem to be able to find it?
At this very moment I am happening to read this from Bruce Payette's "Powershell in Action" (Wonderful book) so share something with you, lucky guy:
"In this example (Poster: an example in the book), even though hello.ps1 is in the current directory, you had to put ./ in front of it to run it. This is because Powershell doesn't execute commands out of the current directory by default. This prevents accidental execution of the wrong command."
Looks like I needed to just put a ".\" on the beginning of the batch file name - not sure if Powershell requires this to execute?

Powershell in CruiseControl.net to backup existing folder before deploying new version of the code

I would like to zip a bunch of files (.exe and .dll) before I overwrite them with the new build. Is there a simple way to zip files without using some sort of dll?
Just creating a folder with the build number / date time stamp will also work great. How do I pass parameters from the cruise control build process into my Powershell script that will do the work then?
Is this a sustainable way to do things?
Thanks
You could either use CCNET's Package Publisher Task directly or zip the files via the PowerShell Task introduced in CCNET 1.5.
Configuration sample for PowerShell Task:
<powershell>
<description>Adding scheduled jobs</description>
<scriptsDirectory>ScheduledTasks</scriptsDirectory>
<script>CreateScheduledJobsFromListOfTasks.ps1</script>
<buildArgs>-zipDir="C:\foo"</buildArgs>
</powershell>