Hi,
Can anyone please convert this vb script to powershell?
' Takes three arguments -- environment, application, server
-- and
' Database.
'====================================================================================================================
Option Explicit
'====================================================================================================================
Dim sWhoami, sUsage, sEnvironment, sApplication, sServer,
sConnectStr, _
iEnvironment, iApplication, iServer,
iSea, iPartnerSea, oConnection, _
iOriginal_iServer, oRS, sPathId,
sTierArch, sdDrive, sDSServer, gstrScriptDir, gobjFSO, sServerRole
sEnvironment = ""
sApplication = ""
sServer = ""
sDSServer = "scm-01"
sPathId = ""
'sTierArch = ""
'sdDrive = ""
'sServerRole = ""
'====================================================================================================================
sWhoami = WScript.ScriptName
sUsage = "Adds a single server to a single
environment-application set:" _
&
vbNewLine _
&
"cscript " & sWhoami & " environment application
server" _
&
" [ pathid 3tier dDrive ]" _
&
vbNewLine
WScript.StdErr.write WScript.Arguments.Count &vbNewLine
If WScript.Arguments.Count < 3 or WScript.Arguments.Count
> 7 Then
WScript.StdErr.Write sUsage
WScript.Quit 1
End If
sEnvironment = WScript.Arguments(0)
sApplication = WScript.Arguments(1)
sServer = WScript.Arguments(2)
If (WScript.Arguments.Count > 3) then
sPathId
= WScript.Arguments(3)
sTierArch
= WScript.Arguments(4)
sdDrive
= WScript.Arguments(5)
End if
If (WScript.Arguments.Count > 6) then
sServerRole
= WScript.Arguments(6)
End if
Set gobjFSO =
CreateObject("Scripting.FileSystemObject")
gstrScriptDir =
gobjFSO.GetParentFolderName(WScript.ScriptFullName)
If gobjFSO.FileExists(gstrScriptDir &
"\DataCenter.EU") Then
sDSServer
= "eu-scm-01"
End If
' See make connection in Lib file.
sConnectStr = "PROVIDER=SQLOLEDB; DATA SOURCE="
& sDSServer & "; DATABASE=ScmWorkbench;
TRUSTED_CONNECTION=YES"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open sConnectStr
Set oRS = CreateObject("ADODB.Recordset")
iEnvironment = GetEnvironment(sEnvironment)
iApplication = GetApplication(sApplication)
iServer = GetServer(sServer)
iSea = GetSea(iEnvironment, iApplication, iServer)
'====================================================================================================================
Function GetEnvironment(sEnvironment)
oRS.Open "select EnvironmentID from
tblEnvironment " & "where Environment = '" &
sEnvironment & "'", oConnection
If oRS.EOF Then
WScript.StdErr.Write sWhoami & ": Could not find environment '"
& sEnvironment & "'" & vbNewLine
WScript.Quit 1
End If
WScript.Echo "Environment '"
& sEnvironment & "' = " & oRS.Fields(0)
GetEnvironment = oRS.Fields(0)
oRS.Close
End Function
'====================================================================================================================
Function GetApplication(sApplication)
oRS.Open "select ApplicationID from
tblApplication " & "where Application = '" &
sApplication & "'", oConnection
If oRS.EOF Then
WScript.StdErr.Write sWhoami & ": Could not find application '"
& sApplication & "'" & vbNewLine
WScript.Quit 1
End If
WScript.Echo "Application '"
& sApplication & "' = " & oRS.Fields(0)
GetApplication = oRS.Fields(0)
oRS.Close
End Function
'====================================================================================================================
Function GetServer(sServer)
Dim sStagingPathId
Dim sThreeTierArch
Dim sHasdDrive
Dim sDefaultHasdDrive : sDefaultHasdDrive
= "y"
Dim sWebServer
oRS.Open "select ServerID from
tblServer where Server = '" & sServer & "'", oConnection
If oRS.EOF Then
oRS.Close
If
IsEmpty(sPathId) then
WScript.StdOut.Write
"Staging path ID (1 for local, 2 for Qwest, 3 for EU, 6 for UK-DR) to add
server '" & sServer & "': "
sStagingPathId
= WScript.StdIn.ReadLine
Else
sStagingPathId
= sPathId
End
If
If
sStagingPathId <> "1" And sStagingPathId <> "2"
And sStagingPathId <> "3" And sStagingPathId <>
"6" Then
WScript.StdErr.Write sWhoami & ": Invalid staging path ID '"
& sStagingPathId & "'" & vbNewLine
WScript.Quit 1
End If
If
IsEmpty(sTierArch) then
WScript.StdOut.Write
"Will '" & sServer _
&
"' exist in a 3-tier architecture? (y/n) "
sThreeTierArch
= WScript.StdIn.ReadLine
Else
sThreeTierArch
= sTierArch
End
If
If sThreeTierArch
<> "y" And sThreeTierArch <> "n" Then
WScript.StdErr.Write sWhoami & "Invalid 3-tier arch response "
& sThreeTierArch & "'" &
vbNewLine
WScript.Quit 1
End If
If
IsEmpty(sdDrive) then
WScript.StdOut.Write
"Does '" & sServer & "' have a D drive? (y/n) ["
& sDefaultHasdDrive & "] : "
sHasdDrive
= WScript.StdIn.ReadLine
Else
sHasdDrive
= sdDrive
End
If
If sHasdDrive =
"" Then
sHasdDrive = sDefaultHasdDrive
ElseIf sHasdDrive
<> "y" And sHasdDrive <> "n" Then
WScript.StdErr.Write sWhoami & "Invalid d-drive response '" &
sHasdDrive & "'" &
vbNewLine
WScript.Quit 1
End If
If sThreeTierArch
= "y" Then
Dim
sPartnerName
Dim
sDefaultPartnerName : sDefaultPartnerName = sServer ' Pre-populate the default
partner name so we can just replace the role(com/web) later.
Dim
sPartnerHasdDrive
Dim sDefaultPartnerHasdDrive
: sDefaultPartnerHasdDrive = "y"
Dim sPartnerWebServer
if
sServerRole = "" Then
WScript.StdOut.Write
"Role of '" & sServer & "' (COM or WEB)? (c/w) "
sServerRole
= WScript.StdIn.ReadLine
End
If
If sServerRole <>
"c" And sServerRole <> "w" Then
WScript.StdErr.Write
sWhoami & ": Invalid COM/WEB server response '" &
sServerRole & "'" & vbNewLine
WScript.Quit
1
ElseIf
sServerRole = "c" Then
sDefaultPartnerName
= replace(sDefaultPartnerName, "com", "web")
Else
sDefaultPartnerName
= replace(sDefaultPartnerName, "web", "com")
End
If
If
sServerRole = "c" Then
sWebServer
= sPartnerName
sPartnerWebServer
= "localhost"
Else
sWebServer
= "localhost"
sPartnerWebServer
= sServer
End
If
WScript.StdOut.Write
"Enter corresponding server name [" & sDefaultPartnerName &
"] : "
sPartnerName
= WScript.StdIn.ReadLine
If
sPartnerName = "" Then
sPartnerName
= sDefaultPartnerName
End
If
oRS.Open
"select ServerID from tblServer " &
"where Server = '" & sPartnerName & "'",
oConnection
If
sServerRole = "c" Then
sWebServer
= sPartnerName
sPartnerWebServer
= "localhost"
Else
sWebServer
= "localhost"
sPartnerWebServer
= sServer
End
If
If
oRS.EOF Then ' partner server not in DB already
oRS.Close
Else
' partner server already in DB
oRS.Close
WScript.Echo
"'" & sPartnerName _
&
"' already exists in tblServer. Please remove it to allow " _
&
"add-server to add it properly with " & sServer & vbNewline
WScript.Quit
1
End
If
WScript.StdOut.Write
"Does '" & sPartnerName _
&
"' have a D drive? (y/n) [" & sDefaultPartnerHasdDrive &
"] : "
sPartnerHasdDrive
= WScript.StdIn.ReadLine
If
sPartnerHasdDrive = "" Then
sPartnerHasdDrive
= sDefaultPartnerHasdDrive
ElseIf
sPartnerHasdDrive <> "y" And sPartnerHasdDrive <>
"n" Then
WScript.StdErr.Write
sWhoami & "Invalid d-drive response '" & sPartnerHasdDrive
& "'" &
vbNewLine
WScript.Quit
1
End
If
If
sHasdDrive = "y" Then
oConnection.Execute
= "insert into tblServer " _
&
"(Server, StagingPathID, WebServer, ServerActive, WebDrive) values "
_
&
"('" & sServer & "', '" & sStagingPathID &
"', '" & sWebServer _
&
"', 1, 'D')"
WScript.Echo
"Added server '" & sServer & "'"
Else
oConnection.Execute
"insert into tblServer " _
&
"(Server, StagingPathID, WebServer, ServerActive) values " _
&
"('" & sServer & "', '" & sStagingPathID &
"', '" & sWebServer _
&
"', 1)"
WScript.Echo
"Added server '" & sServer & "'"
End
If
If
sPartnerHasdDrive = "y" Then
oConnection.Execute
"insert into tblServer " _
&
"(Server, StagingPathID, WebServer, ServerActive, WebDrive) values "
_
&
"('" & sPartnerName & "', '" & sStagingPathID
& "', '" & sPartnerWebServer _
&
"', 1, 'D')"
WScript.Echo
"Added server '" & sPartnerName & "'"
Else
oConnection.Execute
"insert into tblServer " _
&
"(Server, StagingPathID, WebServer, ServerActive) values " _
&
"('" & sPartnerName & "', '" & sStagingPathID
& "', '" & sPartnerWebServer _
&
"', 1)"
WScript.Echo
"Added server '" & sPartnerName & "'"
End
If
Else
' If sThreeTierArch = "n" Then
If
sHasdDrive = "y" Then
oConnection.Execute
= "insert into tblServer " _
&
"(Server, StagingPathID, ServerActive, WebDrive) values " _
&
"('" & sServer & "', '" & sStagingPathID &
"', 1, 'D')"
WScript.Echo
"Added server '" & sServer & "'"
Else
oConnection.Execute
"insert into tblServer " _
&
"(Server, StagingPathID, ServerActive) values " _
&
"('" & sServer & "', '" & sStagingPathID &
"', 1)"
WScript.Echo
"Added server '" & sServer & "'"
End
If
End
If
oRS.Open "select ServerID from tblServer where Server = '" &
sServer & "'", oConnection
End
If
WScript.Echo "Server '" &
sServer & "' = " & oRS.Fields(0)
GetServer
= oRS.Fields(0)
oRS.Close
If sThreeTierArch = "y" Then
oRS.Open
"select ServerID from tblServer where Server = '" & sPartnerName
& "'", oConnection
WScript.Echo
"Server '" & sPartnerName & "' = " &
oRS.Fields(0)
'
Save the original iServer value so we can restore it after the
'
call to GetSea for the second server
' iOriginal_iServer
= iServer
iServer
= oRS.Fields(0)
oRS.Close
iPartnerSea
= GetSea(iEnvironment, iApplication, iServer)
' iServer
= iOriginal_iServer
End If
End Function
'====================================================================================================================
Function GetSea(iEnvironment, iApplication, iServer)
oRS.Open "select SeaIID from
tblServerEnvironmentApplicationXref " _
& "where
EnvironmentID=" & iEnvironment & " and ApplicationID=" _
& iApplication
& " and ServerID=" & iServer
If oRS.EOF Then
oRS.Close
oConnection.Execute
"insert into tblServerEnvironmentApplicationXref " _
& "(EnvironmentID, ApplicationID, ServerID, SeaActive) " _
& "values (" & iEnvironment & ", " &
iApplication & ", " _
& iServer & ", 1)"
WScript.Echo
"Added to tblServerEnvironmentApplicationXref"
oRS.Open
"select SeaIID from tblServerEnvironmentApplicationXref " _
& "where EnvironmentID=" & iEnvironment & " and
ApplicationID=" _
& iApplication & " and ServerID=" & iServer
End If
WScript.Echo "SeaIID = " &
oRS.Fields(0)
GetSea = oRS.Fields(0)
oRS.Close
End Function
'====================================================================================================================