VBS Script to PowerShell script

Rohit Bhan 21 Reputation points
2022-02-03T12:23:14.74+00:00

I need your help to convert this VBS script to PowerShell as we are migrating our application to powershell base and need this to be converted ASAP.

Sub DeleteAdminGroup(strSubComputerName)

on error resume next
err.Clear
Set objAdminGroup = GetObject("LDAP://" & strW10AdminOU)
objAdminGroup.Delete "group", "CN=DEL-GCL-" & strSubComputerName & "-Admin"

if Err.number = 0 then
LogFile.WriteLine "SUCCESS: Deleted computer admin group DEL-GCL-" & strSubComputerName & "-Admin from AD"
else
LogFile.WriteLine "ERROR: Cannot delete DEL-GCL-" & strSubComputerName & "-Admin from AD because " & Err.Description
end if
Err.Clear
end sub

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,091 Reputation points
    2022-02-03T16:10:58.297+00:00

    Try this:

    Function DeleteAdminGroup{
        param(
            [string]$strSubComputerName
        )
        $GroupName = "CN=DEL-GCL-{0}-Admin" -f $strSubComputerName
        Try{
            Remove-ADGroup -Identity $GroupName -SearchBase $strW10AdminOU -ErrorAction STOP
            Out-File $Log -InputObject "SUCCESS: Deleted computer admin group $GroupName from AD"
        }
        Catch{
            OutFile $Log "ERROR: Cannot delete $GroupName $_"
        }
    }
    

    I don't know where the log file name is declared, or where the strW10AdminOU is declared. I'm assuming they somewhere in the script outside the function.

    0 comments No comments

0 additional answers

Sort by: Most helpful