Advertisement
mikedopp

ScheduledTask-ClusterCreation.ps1

Oct 31st, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ############################################################
  2. ############################################################
  3. ### The following script is called from Scheduled task with the action "provision-storage-2016.ps1 cluster01.blah.com"
  4. ############################################################
  5.  
  6. Start-Transcript -Append -Path e:\temp\transcript.txt
  7.  
  8. # Import MySQL Driver and Connect to DB
  9.  
  10.     Import-Module -Name C:\windows\system32\WindowsPowerShell\v1.0\Modules\MySQL\MySql.Data.dll
  11.  
  12.     $ConnectionString = “Server=localhost;uid=blah; pwd=blah;Database=database;”
  13.     $MySQLConnection = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
  14.     $MySQLConnection.ConnectionString = $ConnectionString
  15.     $MySQLConnection.Open()
  16.  
  17.     # Grab most recent pending provision
  18.  
  19.         # Build Query
  20.  
  21.         $query = "SELECT * FROM table WHERE pending=1 ORDER BY date ASC LIMIT 0,1"
  22.  
  23.         # Execute Query
  24.  
  25.         $command     = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $MySQLConnection)
  26.         $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
  27.  
  28.  
  29.         # Query to Array
  30.  
  31.         $table = new-object system.data.datatable
  32.         $dataAdapter.Fill($table)
  33.  
  34.  
  35.  
  36.     # Execute the provision
  37.  
  38.         # Connect and Enter Remote Session Context
  39.        
  40.             $psuser = "domain\user"
  41.             $pspassword = ConvertTo-SecureString "password" -AsPlainText -Force
  42.             $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $psuser, $pspassword
  43.        
  44.             New-PSSession -ComputerName $args[0] -Credential $cred
  45.             Enter-PSSession -ComputerName $args[0]
  46.  
  47.  
  48.         $argumentlist = @($table.username),@($table.password),@($table.provisionstring),@($table.storagepool),@($table.sizeinbytes),@($table.clustergroup)
  49.         $argumentlist | Out-File -Append -FilePath e:\temp\args.txt
  50.         invoke-command -computername @($table.cluster) -FilePath "E:\provision-scripts\provision-smb3-share.ps1" -ArgumentList $argumentlist
  51.  
  52.  
  53.  
  54. ############################################################
  55. ############################################################
  56. NOW THIS FILE IS THE PORTION THAT CREATES A MOUNTPOINT, CREATES AN NTFS VOLUME, AND ADDS THE RESOURCE TO THE "file server" cluster group
  57. ############################################################
  58.  
  59. # Inputs
  60. param(
  61.     [Parameter(Mandatory=$true,Position=1)]
  62.     [string]$username,
  63.     [Parameter(Mandatory=$true,Position=2)]
  64.     [string]$randpass,
  65.     [Parameter(Mandatory=$true,Position=3)]
  66.     [string]$provisionstring,
  67.     [Parameter(Mandatory=$true,Position=4)]
  68.     [string]$storagepoolfriendlyname,
  69.     [Parameter(Mandatory=$true,Position=5)]
  70.     [string]$volumesizeinbytes,
  71.     [Parameter(Mandatory=$true,Position=6)]
  72.     [string]$clustergroup
  73. )
  74.  
  75. # New User
  76.     NET USER $username $randpass /ADD /DOMAIN /Y
  77.  
  78. try{
  79.  
  80. # New Directory to Mount New Volume to
  81.     $mountpoint = "M:\" + $provisionstring
  82.     New-Item -Path $mountpoint –ItemType Directory
  83.    
  84. # New Volume
  85.     new-volume -StoragePoolFriendlyName $storagepoolfriendlyname -friendlyname $provisionstring -FileSystem NTFS -ResiliencySettingName Parity -PhysicalDiskRedundancy 2 -ProvisioningType Fixed -Size $volumesizeinbytes -AccessPath $mountpoint -Verbose
  86.  
  87. # Turn Off Maintenance Mode
  88.     #resume-clusterresource -Name "Cluster Virtual Disk (" + $provisionstring + ")"
  89.  
  90. # Attach Volume to File Server Role
  91.     $fsrole = Get-ClusterGroup | Where { $_.Name -eq $clustergroup}
  92.     #write-host "Cluster Group: " + $fsrole
  93.     $clresource = Get-ClusterResource | where {$_.OwnerGroup -eq "Available Storage" -and $_.Name -eq "Cluster Virtual Disk (" + $provisionstring + ")"}
  94.     #$clresource | Out-File -FilePath m:\provision\output.txt
  95.     #write-host "Cluster Resource: " + $clresource
  96.     Move-ClusterResource -Name $clresource -Group $fsrole
  97.  
  98. # New SMB3 Share
  99.     New-SmbShare -Name $provisionstring -Path $mountpoint -ScopeName $clustergroup -CachingMode None -ChangeAccess $username -ContinuouslyAvailable $true -Description $provisionstring -EncryptData $true -FolderEnumerationMode AccessBased -FullAccess "Domain Admins" -Confirm
  100. }
  101. Catch {
  102.  
  103. #whatever
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement