raducanu
Goto Top

PowerShell Script starten mit externen CMDlets

Guten Tag,

ich bin absolute unerfahren was PowerShell betrifft, muß aber ein Problem lösen.
Hier hoffe ich auch eure Hilfe.

Problem:

Software welches eigene CMDlets mitbringt (DataCore SANsymphony-V). Diese CMDlets sollen aus einer Batchfile genutzt werden (werden aus einem Program angestartet welches nur *.bat oder *.exe Dateien ausführen kann -> APC PowerChute Network Shutdown)

Wenn ich die Powershell der Software starte (powershell.exe mit .ps1 Konfigscript zum registrieren der CMDlets) und dort die Befehle bzw meine *.ps1 ausführe funktioniert alles wunderbar.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -c ". \"C:\Program Files\DataCore\SANsymphony\Register-DcsCmdlets.ps1\""  

Die Register-DcsCmdlets.ps1 sieht wie folgt aus:

param([ScriptBlock]$scriptBlock, [switch]$forceExit, [string[]]$params)

$configurationPath = $Env:TEMP | Join-Path -ChildPath ([Guid]::NewGuid())
New-Item -Path $configurationPath -ItemType Container > $null
@"  
<?xml version="1.0" encoding="utf-8" ?>  
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">  
        <supportedRuntime version="v4.0"/>  
        <supportedRuntime version="v2.0.50727" />  
    </startup>
</configuration>
"@ | Set-Content -Path $configurationPath\powershell.exe.activation_config -Encoding UTF8  

$envVariableName = 'COMPLUS_ApplicationMigrationRuntimeActivationConfigPath'  
$envVariableOld = [Environment]::GetEnvironmentVariable($envVariableName)
[Environment]::SetEnvironmentVariable($envVariableName, $configurationPath)

$importCmdletBlock = { 
	$bpKey = 'BaseProductKey'  
    	$regKey = get-Item "HKLM:\Software\DataCore\Executive"  
    	$strProductKey = $regKey.getValue($bpKey)
    	$regKey = get-Item "HKLM:\$strProductKey"  
    	$installPath = $regKey.getValue('InstallPath')  
	
	Import-Module "$installPath\DataCore.Executive.Cmdlets.dll" -DisableNameChecking -ErrorAction Stop  
	Write-Host "Successfully registered SANsymphony-V Cmdlets for Windows PowerShell."  
}

try
{
    Cls
}
catch
{
    # Nothing to do. This will throw an exception only when it is called
    # without a console.
}

try
{
	if ($scriptBlock -ne $null)
	{
		$finalBlockString = $importCmdletBlock.ToString() + "`n" + $scriptBlock.ToString()  
		$finalBlock = [scriptblock]::Create($finalBlockString)

		if ($forceExit)
		{ & powershell.exe -Command $finalBlock -args $params}
		else
		{ & powershell.exe -NoExit -Command $finalBlock -args $params }
	}
	else
	{
		& powershell.exe -Command $importCmdletBlock -NoExit
	}
}
finally
{
    [Environment]::SetEnvironmentVariable($envVariableName, $envVariableOld)
    $configurationPath | Remove-Item -Recurse
}

Nur wie führe ich meine *.ps1 Datei aus einer *.bat Datei aus, so dass auch die CMDlets der Software zur Verfügung stehen?
Meine *.ps1 Datei
#Konfiguration
$server="SSY-V-01"  
$username="Administrator"  
$password="test123!"  


Connect-DcsServer -Server $server -UserName $username -Password $password -Connection $server
Disable-DcsServerWriteCache -Server $server -Connection $server

Content-Key: 176691

Url: https://administrator.de/contentid/176691

Ausgedruckt am: 29.03.2024 um 08:03 Uhr

Mitglied: Raducanu
Raducanu 23.11.2011 um 15:13:49 Uhr
Goto Top
Ich bin etwas weiter gekommen:

Mit

#Konfiguration
$server="SSY-V-01"  
$username="Administrator"  
$password="test123!"  

Import-Module 'C:\Program Files\DataCore\SANsymphony\DataCore.Executive.Cmdlets.dll"  
Connect-DcsServer -Server $server -UserName $username -Password $password -Connection $server
Disable-DcsServerWriteCache -Server $server -Connection $server

Startet zumindest eine PS mit DataCore CMDlets.
Nur die Befehle Connect-DcsServer und Disable-DcsServerWriteCache werden erst ausgeführt wenn ich mit "exit" die DataCore CMDlet Ebende wieder verlasse.