Category Powershell

Lately, I’ve been doing a lot of work provisioning and decommissioning LUNs on some Fibre Channel arrays. As you all know, this process can be somewhat tedious when provisioning a new LUN on vSphere or removing said LUN.

In the past, I would always use this PowerCLI command

PowerShell

1

Get-VMhost-Cluster"Cluster1"|Get-VMHostStorage-Refresh

This would first get all the ESXi hosts in the cluster “Cluster1” and then launch the refresh cmdlet on each host. The problem with this is that it can take a really long time for the command to complete because the refresh is executed host by host. Before starting the next refresh, the command waits for the previous one to finish. While this is fine for smaller environments, the time really does add up when you have a decent sized cluster.

Using PowerShell jobs

In order to reduce the time this process takes, I decided this would be a great use case for PowerShell jobs. A job is essentially another PowerShell instance that runs in the background. This means that your main script or PowerShell window doesn’t wait for it to finish before going to the next step. Using jobs will allow us to start the rescan on each host as a job and then start the next job. Meaning the rescans will happen in parallel.

Code

This will be another short one, but I figured someone else will have run into this.

While I was doing a rework for a Veeam implementation, I noticed on several jobs that there were exclusions set inside the jobs. I wanted a list of all jobs with their respective exclusions, time for Powershell!

The script starts by getting a list of all Veeam jobs. Next, it will go through all jobs and look for objects that have the type “Exclude” set. What follows is a bit of code to match the job name to the different exclusions and dump everything into a CSV. I struggled a bit with getting the contents of the array listed properly in the CSV, I kept getting the array listed as “System Object[]“. Turns out I just needed to put the $VMExclusions variable between quotes.

The CSV will look something like this, the job name is listed on the left and the excluded objects on the right.

One last note, this script needs to be run from the Veeam server itself.

As always, you can find the most recent version of the script on Github. The initial version can be found below.

A couple of our web servers were running into some issues with disk space. Turns out the logs weren’t being cleaned up properly.
In order to remediate this, I wrote a function that can be reused anywhere.

The function accepts 3 parameters:

FilePath: The directory where you want to remove the logs from

CutOff; Specifies the age (in days) that a file must have before being deleted.

LogPath; This parameter specifies the directory where you want the CSV log file. If this is left open, no CSV will be saved.

Example

As an example, I’m going to remove all files, older than 30 days, from the folder “C:\temp\W3SVC2030036971\W3SVC2030036971\”. I want a CSV log to be written in the “C:\temp” folder.

A view from the Powershell window

As you can see, you always get feedback in your window, even if you don’t specify a log path.

The CSV looks something like this:

Code

You can find the script code below. This is provided as-is. You can find the most recent version of the code on GitHub.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

#Requires -Version 3.0

FunctionRemove-Logs{

<#

.SYNOPSIS

Delete old log files

.DESCRIPTION

This function will delete all .log and .txt files older than a certain number of days.

You have the ability to specify log path, extension (log or txt) and path.

.NOTES

Author: Maarten Van Driessen

.PARAMETER FilePath

Specify the path where you want to delete the logs from.

.PARAMETER CutOff

Specify how many days you want to go back.

.PARAMETER LogPath

Where do you want to store the log file. Path must end with a \

.PARAMETER FileExtension

Specify whether you want to delete *.log, *.etl or *.txt files. If left blank, the function will delete both.

Must be written as *.log, *.etl or *.txt

.EXAMPLE

Remove-Logs -FilePath C:\inetpub\ -Cutoff 30 -LogPath c:\temp\

Delete all txt and log files older than 30 days from the c:\inetpub folder and write the log to c:\reports