Failed to Import-Module MSOnline in ASP.NET - asp.net

We have created a mini web program for running PowerShell in asp.net. the PowerShell command is running fine. but when we try to import the MSOnline module, it will be shown the following error:
Could not load file or assembly
'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll'
or one of its dependencies. An attempt was made to load a program with
an incorrect format.
Dim Ps As PowerShell = PowerShell.Create()
Dim tempScript As String = "Import-Module MSOnline " + Environment.NewLine
tempScript = tempScript + "$username = 'xxx#xxx.com' " + Environment.NewLine
tempScript = tempScript + "$password = 'abcd1234' " + Environment.NewLine
tempScript = tempScript + "$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force " + Environment.NewLine
tempScript = tempScript + "$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd " + Environment.NewLine
tempScript = tempScript + "Connect-MsolService -Credential $creds " + Environment.NewLine
tempScript = tempScript + "Get-MsolDomain" + Environment.NewLine
Ps.Commands.AddScript(tempScript)
' Execute the script
Dim results = Ps.Invoke()
PS C:> $PSVersionTable | Out-String
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
Update : 3-Aug-2017
Try to use process.start to call a CMD, and then the cmd call a powershell, but it failed also. same error message.
Private Function RunBatch() As String
Dim proc As New Process
Dim strBuilder As StringBuilder = New System.Text.StringBuilder
proc.StartInfo.FileName = "C:\temp\enableMFA.bat"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
proc.WaitForExit()
Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
For Each ln As String In output
strBuilder.Append(ln & vbNewLine + vbLf)
Next
Return strBuilder.ToString
End Function
Error Message:
C:\Users\admin\Desktop\Preview\Preview\bin\Debug>powershell -File
"C:\temp\enableMFA.ps1"
Import-Module : Could not load file or assembly
'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll'
or
one of its dependencies. An attempt was made to load a program with an
incorrect format.
At C:\temp\enableMFA.ps1:1 char:1
Import-Module MSOnline
~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [Import-Module], BadImageFormatException
FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Finally fixed the issue by change the CPU to x64, this is the steps
Right click the project >> properties >> complie >> Target CPU to x64
Go to TOOLS >> Options >> Projects and Solutions >> Web Projects >> use 64bit IIS Express
** Remember close the VS2013 and open the project again **
** Thx for the hints from David Brabant**

Related

Use slash option in R system function

I am trying to use following cmd function inside R
rmdir /q /s myDir
I tried:
system("rmdir /q /s myDir")
but it returns an error:
rmdir: failed to remove '/q': No such file or directory
rmdir: failed to remove '/s': No such file or directory
rmdir: failed to remove 'public_html/croecon.contentio.biz': Directory not empty
How should I use slash option, e.g. /q?
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery("SELECT * FROM Win32_ProcessStartTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & objReceivedEvent.ProcessID)
For Each objItem in colItems
wscript.echo objItem.name & " " & objItem.ProcessID & " " & objItem.CommandLine
Next
Loop
Then type in a command prompt
cscript //nologo C:\folder\MonitorProcessCreation.vbs
This will monitor what starts when you run your command.

Execute .jar file in .net core on Openshift container platform

net core application which internally calls a java (.jar) file using .net Process class. I have used cmd.exe to run .jar file along with parameters. I have deployed this application on Openshift Container Platform. But as openshift is running on Linux, so cmd.exe is not available.
Below is code in .net core to execute jar file.
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WorkingDirectory = Common.JarWorkingDir;
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/K java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"');
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
cmd.StandardInput.WriteLine("exit");
cmd.StandardInput.Flush();
cmd.WaitForExit();
So Jar file is unable to execute. Any alternative to execute this jar with .net on OpenShift. Please help.
OpenShift is essentially Kubernetes running Linux containers. In other words, your code should act as if it is running on Linux.
Instead of cmd.exe, use bash (or, sh, or really, whatever shell is pre-installed in your container):
Process cmd = new Process();
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WorkingDirectory = Common.JarWorkingDir;
cmd.StartInfo.FileName = "bash";
cmd.StartInfo.Arguments = "-c 'java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"'');
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
cmd.StandardInput.WriteLine("exit");
cmd.StandardInput.Flush();
cmd.WaitForExit();
You can maybe even remove some of the lines. CreateNoWindow, for example, is not required because .NET Core does not create windows on Linux at all.
If you have no shell expressions, perhaps you can even get things to be simpler and simplify from this:
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = "bash";
cmd.StartInfo.Arguments = "-c 'java -jar " + string.Format("{0} {1}", '"' + Common.JarFilePath + '"', '"' + sourceCodePath + '"'');
to something like this:
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.FileName = "java";
cmd.StartInfo.Arguments = $"-jar \"{Common.JarFilePath}\" \"{sourceCodePath}\"";
Oh, and watch out for the quoting in your Arguments variable. If you wrap the entire command after -c with single quotes, you should be fine, but if you are doing something trickier - if Common.JarFilePath isn't a simple file name - it may not work so well. Definitely test and tweak that. Maybe consider EscapeAndConcatenate.

How to change LogFile.Directory property via WMI on a remote computer?

I was able to retrieve, using WMI, the value of LogFile.Directory for a specific IIS site located on a remote computer.
Now I need to change the LogFile.Directory property - this is a step in an automation process - but I am hitting a wall. This is what I have so far, although it doesn't work.
Write-Output "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
$site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
If ($site.Name -eq $IISSiteName) {
Write-Output "Found site $IISSiteName"
Write-Output ("Existing log folder: " + $site.LogFile.Directory)
$newLogFolder = "E:\" + $IISSiteName + "\logs"
Write-Output ("Set IIS log folder to " + $newLogFolder)
$site.LogFile.Directory = $newLogFolder
$site.Put()
}
I don't get any errors. Simply the LogFile.Directory value does not change on the remote machine when I check in IIS Manager.
I've read that I should be using Set-WMIInstance instead, so I tried:
Write-Verbose "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
$site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
$sitePath = (Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'").__path
If ($site.Name -eq $IISSiteName) {
Write-Output "Found site $IISSiteName"
$newLogFolder = "E:\" + $IISSiteName + "\logs"
Write-Output ("Existing log folder: " + $site.LogFile.Directory)
Set-WmiInstance -Path $sitePath -argument #{LogFile.Directory = $newLogFolder}
Write-Output ("Set IIS log folder to " + $newLogFolder)
}
But this throws errors:
At E:\\Test.ps1:71 char:54
+ Set-WmiInstance -Path $sitePath #{LogFile.Directory = $newLogFolder}
+ ~
Missing '=' operator after key in hash literal.
At E:\\Test.ps1:72 char:18
+ Write-Output ("Set IIS log folder to " + $newLogFolder)
+ ~
Missing '=' operator after key in hash literal.
At E:\\Test.ps1:41 char:54
+ foreach ($FSMappingNode in $FSMappingNodesArray) {
+ ~
Is there a way to change this particular value remotely or is it a read-only property?
Any help would be greatly appreciated.
Thanks,
// Francesco
After much tinkering, I finally found the way to change the LogFile.Directory of an IIS site remotely:
Write-Output "Making IIS connection to $($XmlCloudNodeFullyQualifiedDomainName)"
$site = Get-WmiObject -ComputerName $XmlCloudNodeFullyQualifiedDomainName -Namespace "root/webadministration" -Class Site -Authentication 6 -Filter "Name = '$IISSiteName'"
$sitePath = $site.__path
Write-Verbose $sitePath
If ($site.Name -eq $IISSiteName) {
Write-Output "Found site $IISSiteName"
Write-Verbose ("Existing log folder: " + $site.LogFile.Directory)
$newLogFolder = "E:\" + $IISSiteName + "\logs"
$siteLogFile = $site.LogFile
$siteLogFile.Directory = $newLogFolder
Set-WmiInstance -Path $sitePath -Arguments #{LogFile = $siteLogFile}
Write-Output ("Set IIS log folder to " + $siteLogFile.Directory)
}

Run SSIS package in asp.net using powershell

Can anyone tell me how to run SSIS package with powershell on the server in asp.net. Please note i need to specify config file path also.
var processStartInfo = new ProcessStartInfo
{
Arguments = "dtexec /F " + "\"" + pkgPath + packageName +"\"",
FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
};
Process process = Process.Start(processStartInfo);
How to specify the config file path in the above code.

Custom rubble in Aptana not activated automatically

I am new to Aptana, so I could be missing something obvious. My problem in that bundle I created does not get activated when Aptana is loaded. I have to update date of the rubble.rb file for it to work.
touch ~/Documents/Aptana Rubles/ioncubeEncode.ruble
After I do that, for the rest of that Aptana session things are peachy.
I do not think it is relevant, but here is bundle code, which works just fine (when it works):
require 'ruble'
#Ruble::Logger.log_level = :trace
bundle do |bundle|
bundle.author = "Sasha"
bundle.copyright = "None"
bundle.display_name = "Ioncube Encode"
bundle.description = "Encode just saved php file form base_local to base folder"
end
command "Ioncube Encode" do |cmd|
cmd.input = :document
cmd.output = :output_to_console
cmd.trigger = :execution_listener , "org.eclipse.ui.file.save"
cmd.invoke do |ctx|
source_path = ENV['TM_FILEPATH']
ext = File.extname(source_path)
if ext == '.php'
if ( source_path =~ /base_local/)
destination_path = source_path.sub('/base_local/','/base/')
#CONSOLE.puts "Ioncube Encoding: " + source_path + " to " + destination_path
exec="/usr/local/ioncube/ioncube_encoder5 -v --optimize more --without-loader-check " + source_path + " -o " +destination_path + " 2>&1"
IO.popen(exec, 'r+') do |io|
io.close_write
CONSOLE.puts io.read
end
end
end
end
end
This might not seem like a big deal but it bothers me - a lot.

Resources