Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PowerShell Scripting help to combine two scripts

$
0
0

I have a script that helps automate virtual server builds.  The script creates a specification document which is used to perform the actual builds.

# Spec-And-Build.ps1 - Loops through a directory and performs an
# end to end specification generation and build on each request in that directory

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true)]
    [string]$RequestDir,
    [Parameter(Mandatory=$true)]
    [string]$SpecDir,
    [Parameter(Mandatory=$false)]
    [pscredential]$Credential_CPR,
    [Parameter(Mandatory=$false)]
    [pscredential]$Credential_VDC,
    [Parameter(Mandatory=$false)]
    [pscredential]$Credential_Admin
)

if (!$Credential_CPR) {
    $Credential_CPR = Get-Credential -Message "CPR Domain Credentials."
    if (!$Credential_CPR.UserName.ToUpper().StartsWith("CPR\")) {
        $Credential_CPR = New-Object pscredential(
            ("CPR\" + $Credential_CPR.UserName), $Credential_CPR.Password)
    }
}

if (!$Credential_VDC) {
    $Credential_VDC = Get-Credential -Message "VDC Domain Credentials."
    if (!$Credential_VDC.UserName.ToUpper().StartsWith("VDC\")) {
        $Credential_VDC = New-Object pscredential( `
            ("VDC\" + $Credential_VDC.UserName), $Credential_VDC.Password)
    }
}

if (!$Credential_Admin) {
    $Credential_Admin = Get-Credential -UserName "Administrator or root" `
        -Message "Enter the Administrator or root password for the new VM. The user name here will be ignored"
}

.\Gen-VMSpec-Many.ps1 -RequestDir $RequestDir -SpecDir $SpecDir -Credential_VDC $Credential_VDC

$caption = "Continue Build"
$message = "Please validate the generated specification files in $SpecDir - Then click Continue to start build"
$opContinue = new-Object System.Management.Automation.Host.ChoiceDescription "&Continue","help"
$opAbort = new-Object System.Management.Automation.Host.ChoiceDescription "&Abort","help"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($opContinue,$opAbort)
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

if ($answer -eq 0) {
    .\Build-VM-Many.ps1 -SpecDir $SpecDir -Credential_CPR $Credential_CPR -Credential_VDC $Credential_VDC -Credential_Admin $Credential_Admin
} else {
    Write-Host "Build step aborted, run Build-VM-Many.ps1 manually to continue."
}

This script works well.  Now I need to add a section that adds Active Directory groups to the built servers from the spec document.  I found this script which also works well:

# Create local group on the local or a remote computer 
 
Write-Host -foregroundcolor Yellow 'Admin Privileges Required!' 
 
$computerName = Read-Host 'Enter computer name or press <Enter> for localhost' 
$localGroupName = Read-Host 'Enter local group name' 
$description = Read-Host 'Enter description for new local group' 
 
if ($computerName -eq "") {$computerName = "$env:computername"} 
 
if([ADSI]::Exists("WinNT://$computerName,computer")) { 
 
    $computer = [ADSI]"WinNT://$computerName,computer" 
 
    $localGroup = $computer.Create("group",$localGroupName) 
    $localGroup.SetInfo() 
    $localGroup.description = [string]$description 
    $localGroup.SetInfo() 
}

My question is, how do I make one script from the two?  I am very new to PowerShell scripting.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>