Archive

Archive for the ‘Scripts’ Category

Docker for Windows start-up error

September 25th, 2016 1 comment

After installing Docker for Windows I was unable to run it, following error appeared.

elmore_blog_docker_windows_powershell01

Unable to create: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The specified module 'Hyper-V' was not loaded because no valid module file was found in any module directory.
at <ScriptBlock>, <No file>: line 76
 at Docker.Backend.HyperV.RunScript(String action, Dictionary`2 parameters)
 at Docker.Backend.ContainerEngine.Linux.Start(Settings settings)
 at Docker.Core.Pipe.NamedPipeServer.<>c__DisplayClass8_0.<Register>b__0(Object[] parameters)
 at Docker.Core.Pipe.NamedPipeServer.RunAction(String action, Object[] parameters)</pre>

I checked that Hyper-V was installed but it seems that Docker also needs to have the Powershell tools installed. So activate the feature and Docker will run fine 🙂

elmore_blog_docker_windows_powershell02

VMWare PowerCLI script: increase RAM and update VMWare Tools

January 11th, 2016 No comments

The other day I had to increase the memory of 30 virtual servers and install the latest VMWare tools. Instead of doing this manually I uses PowerCLI.

Put the serverlist in a *.txt file that is located in the same folder as the PowerShell script. This will shutdown the server, increase the RAM to 4GB, start the server and then upgrade the VMWare tools.

$servers = Get-Content -Path E:\path_to_my_folder\server_list.txt

get-vm $servers | Shutdown-VMGuest –Confirm:$False

sleep 150

Get-VM $servers | Set-VM –MemoryGB 4 –Confirm:$False | Start-VM

sleep 120

Get-VM $servers | Update-tools -RunAsync

Sourcetree git diff script

August 4th, 2015 No comments

Git is great but it’s not always easy to know which files were changed between commits. I don’t want to deploy all files with each release so I wrote a small script that I use as a custom action in Sourcetree. It’s a batch script that takes 3 parameters:

  • the local path of the repository
  • hash of selected commit #1
  • hash of selected commit #2

sourcetree_git_diff_custom_action01

Batch script is added below, you’ll just need to modify the path to your local git.exe file
it contains a fix for windows users to replace the forward slash of git diff by a backslash

@echo off
setlocal EnableDelayedExpansion

C:\Users\&lt;myusername&gt;\AppData\Local\Atlassian\SourceTree\git_local\bin\git.exe diff --name-only %2 %3 &gt; "C:\temp\a.txt"

mkdir C:\temp\%2
for /F "tokens=*" %%b in (c:\temp\a.txt) do (

set "lineString=%%b"
set "to_replace=/"
for %%f in ("!to_replace!") do set "str2=!lineString:%%~f=\!"
REM echo "%1\!str2!"

ECHO Processing "%1\!str2!" ....

xcopy "%1\!str2!" "C:\temp\%2\!str2!*" /s /i /Y
)

endlocal

Select the 2 commits and execute the custom action. The modified files will be copied under C:\temp\<hash1>\

sourcetree_git_diff_custom_action02

PowerShell: count items per folder

August 26th, 2010 No comments

This script is created to count how many folders exist in a NAS Share. It loads the list from SQL Server and writes the result back to the same table. You can modify it to your own needs.

# **********************************************
# Created by SĂ©bastien Morel
# http://blog.elmore.be
# Last update: August 26th, 2010
# Version 1.00
#
# This script will loop NAS Folders
# and return number of homefolders per share
# **********************************************

# retrieve NAS Folders
$sqlsvr = '&lt;MySQLServer&gt;'
$database = '&lt;MySQLDB&gt;'
$table = 'tbl_homeshares_trees'

Clear-Host
Write-Host &quot;Please wait a moment, this can take a while...&quot;
Write-Host &quot; &quot;

#Create SQL Connection
Write-Verbose &quot;Creating SQL Connection...&quot;
$conn = New-Object System.Data.SqlClient.SqlConnection(&quot;Data Source=$sqlsvr;Initial Catalog=$database; Integrated Security=SSPI&quot;)
$conn.Open()
$conn2 = New-Object System.Data.SqlClient.SqlConnection(&quot;Data Source=$sqlsvr;Initial Catalog=$database; Integrated Security=SSPI&quot;)
$conn2.Open()

$cmd = $conn.CreateCommand()
$cmd2 = $conn2.CreateCommand()

$cmd.CommandText = &quot;select tree_id, tree_path from $table&quot;
$Reader = $cmd.ExecuteReader()

while ($Reader.Read()) {
	$sID = $Reader[&quot;tree_id&quot;].Tostring()
	$sPath = $Reader[&quot;tree_path&quot;].Tostring()
	Write-Host &quot;Querying $sPath...&quot;
	$sGo = Get-ChildItem $sPath
	# update table
	$cmd2.CommandText = &quot;UPDATE $table SET tree_count = &quot; + $sGo.Count + &quot; WHERE tree_id = '&quot; + $sID + &quot;'&quot;
	$cmd2.ExecuteNonQuery() | Out-Null
}

$conn.Close()
$conn2.Close()
$Reader.Close()

The results in the database

Categories: Powershell Tags: ,

PowerShell: delete files older than x days

August 25th, 2010 1 comment

Classical script and we all need it some day. Edit the folder path and adjust the number of days to your clean-up schedule. The script works recursive so it will also clean-up subfolders.

As always, make sure you can execute PowerShell scripts

Set-ExecutionPolicy RemoteSigned

The Script…

$Now = Get-Date
$Days = &quot;30&quot;
$TargetFolder = &quot;C:WINDOWSsystem32LogFiles&quot;
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.log, *.txt -recurse | Where {$_.LastWriteTime -le &quot;$LastWrite&quot;} 

foreach ($File in $Files){
	write-host &quot;Deleting File $File&quot; -foregroundcolor &quot;Red&quot;;  Remove-Item $File | out-null
}
Categories: Powershell Tags: , ,

Powershell WMI Explorer

August 20th, 2010 No comments

WMI was in the past commonly used for scripting tasks and sometimes it handy to know which classes are installed on a server/workstation. Today I found a WMI GUI that was written in Powershell by Marc van Orsouw. He has a blog called The PowerShell Guy and you can download it from here!

Categories: Powershell Tags:

Powershell v2 released for pre Windows 7 machines

August 12th, 2010 No comments

Windows Management Framework, which includes Windows PowerShell 2.0, WinRM 2.0, and BITS 4.0, was officially released to the world this morning. By providing a consistent management interface across the various flavors of Windows, we are making our platform that much more attractive to deploy. IT Professionals can now easily manage their Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7, and Windows Server 2008 R2 machines through PowerShell remoting – that’s a huge win!

You can download the packages here: http://go.microsoft.com/fwlink/?LinkID=151321

Categories: Powershell Tags:

Powershell: check installed version

August 12th, 2010 No comments

It might be usefull to know which version of Powershell is installed on your server / desktop.
Open a dos-prompt and then open a PowerShell session. There are 3 ways to check the installed version

Method 1

$Host.Version

Method 2

get-host


Method 3 (works only with v2)

$PSVersionTable

Categories: Powershell, Scripts Tags: , ,

Script to add DNS suffixes to a NIC

August 11th, 2010 2 comments

Adjust the array with your dns suffixes and save as a .VBS file

DNSSuffixSearchOrder = Array(“MYDNS1.COM”, “MYDNS2.COM”)

Set WMI = GetObject(“WinMgmts://”)

Set adapter = GetObject(“winmgmts:Win32_NetworkAdapterConfiguration”)

adapter.SetDNSSuffixSearchOrder (DNSSuffixSearchOrder)

Archive post Jan 7th, 2007

Categories: Scripts, VBScript, Windows Tags: ,