Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

General script layout/structure and deployment

$
0
0

So this has been bugging me for a while, and I thought I’d see what everyone else was doing. Let’s say you have a script which needs to run as a scheduled task, and this script needs to log into a bunch of servers, pull a some kind of data, manipulate the data, then put it into SQL. I have 15+ of these (running on various servers) and don’t feel very comfortable with how I’m structuring each one. I can’t put my finger on why, but it just doesn’t feel right.

I usually create each script as a series of functions, as an example

function Get-ServerList() # Log into AD/read a CSV/whatever and get a list of servers function Get-ServerInfo() # logs into server(s) and returns an object with whatever data we're looking for function Manipulate-ServerInfo() # takes the previous object and applies whatever logic we need. # returns a new object ready for insertion into SQL function Write-ServerInfoToSQL() # connects to SQL Server, writes the data, disconnects from SQL 

At the end of the script I then call these functions

$ServerList = Get-ServerList $ServerInfo = Get-ServerInfo -Servers $ServerList $FinalData = Manipulate-ServerInfo -ServerInfo $ServerInfo Write-ServerInfoToSQL -Data $FinalData 

The scheduled task then runs the PowerShell file. There will usually be a try/catch in there to email me any errors, or a Start/End-Transcript where it emails me all output.

I usually write each of these functions as advanced functions, even though a user will never be running them individually and where piping in data will never be needed. Does it make sense to still use advanced functions here, with the full Begin/Process/End sections? I know the benefits of -Verbose and -Debug, but I very rarely use these. It seems like unnecessary boilerplate.

I realise I could also set this script up as a module, and not need to have the final section where I call each function, but could rather just have a single function that calls the others and just run the CMDlet, but there don’t seem to be any benefits to this approach if using scheduled tasks, and it makes development more difficult, without much benefit.

The scripts are all in SVN (yeah, yeah, it’s an old repo…) for development, but once they’re ready for production, and any subsequent updates are manually copied to the servers where they will actually run. Is there a better way to do this? Something to note is that many of these servers are on different domains so sticking them in a single share isn’t really an option.

Long story short, how do you deal with scenarios like this? Is there anything obvious which I’m doing poorly?

submitted by /u/Henry_Horsecock
[link] [comments]

The post General script layout/structure and deployment appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles