Functions, Scripting Technique, Uncategorized

User Back up function or Script

 

First I want to point out the $home variable for individual self initiated back ups.

so if you wanted to back up specifics to the current logged in user $home is Sufficient, even if you have DFS / or SMB redirected home folders.

if you are backing up all users you need to also save the sticky notes. that’s an added bonus to reading my article.

Function Get-Backup ($user){

# Get logged on Username
if ($User -like $null) {$User = $env:username}

Create path using variables so that we arent guessing usernames.
$path = "\\Server\Users\$user\user_backup\$(Get-Date -Format M)\"

#create newer Path based on the date.
new-item -ItemType directory -Path $path -ErrorAction SilentlyContinue
$logged ="$path\Results.txt"

#copy contents of vital folders specified below from there paths to the new location.

copy-item c:\users\$user\pictures -recurse -Destination $path -ErrorAction SilentlyContinue
copy-item c:\users\$user\documents -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\downloads -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\favorites -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\links -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\desktop -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\music -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\contacts -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
# I only included these folders because these are standard folders anything extra needs to be added manually.

I know a lot of people who are attached to their sticky notes.
located here:
$notes= ls "C:\Program Files\WindowsApps\microsoftstickynotes*"
copy-item $notes.fullname $path -Verbose -ErrorAction SilentlyContinue
}#End Function  you can always make this much more robust.

 

Enjoy City Life? I grew up in the city and I rather not deal with heavily populated areas. although I prefer people than bugs that goes for computer bugs too lol

Functions, Scripting Technique, Uncategorized

PowerShell Get-TimeMachine?!

Well, I know what you’re thinking, it is just click bait. But I want to assure you it is possible.

new-timespanYou could create this into a function but the magic is in the details of this one.

“New-TimeSpan” is a tool that has existed since 2.0 and before you start-sleep ever again, consider the possibilities if you can truly calculate the duration of start-sleep dynamically!

So I stumbled across this only due to the need to simulate a scheduled task/job quickly in an environment I had not the local privileges to ensure that I could create a scheduled job.

If necessary you could utilize this to create some uniformity in your daily duties, reminders, etc.

 

So Let’s see this tag team with Start-Sleep!

tagteam timespan

I included the variable to display how this works so well. you just define when you want the script to stop that same day. It will show an error if you did it differently.  However, as long as the values are a positive number and not a negative integer you have a timespan.

The last example is using it to control things in the future….essentially a PowerShell Time machine, please share any of your experiences in the comments or on Twitter.

timemachine

so just know that time only flows in one direction. Trying to relive the past can cause you errors lol. Kind of ironic how that works outside the shell too. Don’t forget to visit other parts of the city. See ya around, and avoid cyclic redundancy errors!

Functions, Scripting Technique

PowerShell and utilizing it’s Profile.

It may not be something we all wake up in the morning thinking about but to wake up and make your life just a bit easier is something we all think about. probably at more times through out the day.

The PowerShell profile that is going to be covered are both user based profiles.

One is the Shell Profile and the other is the ISE Profile.

both profiles are saved via the $profile defined path.

shell_Profile
Shell
ise_profile
ISE

These are essentially scripts with your preferences, functions, load frequently used modules, favorite variables, and etc.

I have been in some interesting situations where I automated report building into functions. Load specific modules that I use daily and some routine $variables to load that are always the same.

imagine how much time saving it would be to have your cup of coffee sitting and waiting on you in the car already made? that is the advantage the PowerShell Profile provides. A series of tasks, preparations that come automatic every time you click the sweet PowerShell emblem.

if you have network mapped user home folder, your profile can follow you to any and every server/machine you work on.

experiment and have fun with making your life easier.

any questions or comments let me know how you use your $profile !

Functions, Scripting Technique

Comparing objects, files, and variables in PowerShell

How do you Compare objects?

Most people throw things at a problem and see what sticks.

Is this a true “solution” or a makeshift glue that can come unraveled at any given moment.

 

I think that when we compare we should understand what is needed and not needed, and really understand our options.

Understand the basics

This is an example of a variable with text defined. we want to look for the word Fruit.

PS C:\> $Apple = "Red Fruit, Green Fruit, Yellow Fruit"

PS C:\> $Apple -match 'Fruit'
True

PS C:\> $Apple | where {$_ -match 'Fruit'}
Red Fruit, Green Fruit, Yellow Fruit

PS C:\> $Apple -like 'Fruit'
False

PS C:\> $Apple | where {$_ -like 'Fruit'}

 

So it looks like “Like” doesn’t like to be used in the way you cognitively expect.

PS C:\> $apple -like '*Fruit*'
True

PS C:\> $apple | where {$_ -like '*Fruit*'}
Red Fruit, Green Fruit, Yellow Fruit



Well there is a trick to it. The trick is it has rules that govern how it looks at the compared objects and if it care about what comes before or after the string you are looking for.

I like to use match. some instances like the Active Directory module that comes with RSAT doesn’t allow it with the -filter parameter. you can pipe and filter instead.

Take a look below:

PS C:\> get-aduser -filter {givenname -like *Xajuan*}

VS the pipeline equivalent below:

PS C:\> get-aduser -filter * | where {$_.givenname -match

Either way stay true to your comfort zone and be sure to explore outside it, because at the end of the day, you got to get the job done, and nothing compares like that is complete.

Functions, Scripting Technique, Uncategorized

Writing Functions in PowerShell

functioning like an adult

I would like to propose we should all function as adults at some point in life. maybe not 24 hours a day but enough to take care of ourselves.

PowerShell is an area that need you to be functioning to preserve your sanity and time, I will cover simple & advanced functions.

A function can save you a ton of typing and it could do quite a bit and be called using the infamous tab completion!

Simple Function Example:

Function Test-Google
{
Try{ $result=Test-Connection google.com
if ($result -ne $null){ write-host "Google is up and reachable" -ForegroundColor Green} 
}
Catch{write-host "Google Error Occured" -ForegroundColor Red}

}

 

Advanced Function Example:

Function Test-NetworkConnectivity
{
[cmdletbinding()]
param(
[parameter(ValueFromPipelineByPropertyName,ValueFromPipeline)][Object[]]$Website =@( "google.com","Bing.com")
)
Begin{}

Process{
FOREACH($SITE IN $Website)
{
Try{ $result=Test-Connection -ComputerName $site
if ($result -ne $null){ write-host "$site is up and reachable" -ForegroundColor Green} 
}
Catch{write-host "$site Error Occured" -ForegroundColor Red}
}
}
}

Take the time to explore tab completion.  this advanced function has way more capability and any questions?