netsh interface set interface name="Local Area Connection 2" admin=DISABLED - c#

I want to disable all network adapters:
I saw that it can be done via system command netsh.
When I run the following command i got an error.
C:\Windows\system32>netsh interface set interface name="Local Area Connection 2" admin=DISABLED
Area is not an acceptable value for connect.
The parameter is incorrect.
It seems that the command line parser could not get the spacing. The same happens if using single quote (instead of double quotes).
i need your help gurus.
By the way, my code is C#, I tried many examples from the web and none of worked.
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(
"netsh",
"interface set interface name=\"" + interfaceName + "\" admin=enabled");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
bool result = p.Start();

I use
netsh interface set interface "Local Area Connection 2" ENABLED
it works fine for me and enables "Local Area Connection 2"
probably you might use this way with your code, it should work for you.

echo ----------------------------------------------
echo Restarting network interfaces
echo ----------------------------------------------
netsh interface ipv4 show interfaces
for /F "tokens=1-3,4*" %%I in ('netsh interface ipv4 show interfaces ^| find /I "CONNECTED" ^| find /I /V "DISCONNECTED" ^| find /I /V "LOOPBACK"') do (
echo.&echo - Restarting #%%I [%%M] ...
netsh interface set interface name="%%M" admin=disabled >nul 2>&1
ping -n 1 127.0.0.1 >nul 2>&1
netsh interface set interface name="%%M" admin=enabled >nul 2>&1
ping -n 1 127.0.0.1 >nul 2>&1
if %ERRORLEVEL% EQU 0 (
echo +++ OK +++
) else (
echo --- NOK ---
)
)

Related

Service installer with a different name

I am using below cmd code to install a windows service
#echo off
:: BatchGotAdmin
::-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------
::ENTER YOUR CODE BELOW:
cmd /k "C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil -i C:\fgbk\WindowsService\installer\OurIntegrationService.exe"
So this creates a service with name OurIntegrationService and i want to create another instance of same service with a different name OurIntegrationServiceStage
How can i do this ?
The ServiceName identifies the service to the Service Control Manager.
The value of this property must be identical to the name recorded for
the service in the ServiceInstaller.ServiceName property of the
corresponding installer class. In code, the ServiceName of the service
is usually set in the main() function of the executable.
https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase.servicename?view=netframework-4.7.2
To give a different name, I suggest you change the service (if you have the code) to read name from app.config, or any other config location.
You can also change the display name (not the service name) by directly changing it in registry.

ILmerge does not implement Newtonsoft.json

The second day I beat my head against the wall and can not figure it out.
How it is possible to implement this dll, any mistakes constantly jump out.
I tried many different options that are on the Internet. Nothing helped.
Help me figure out how to solve the problem.
How to implement Newtonsoft.Json in my project via ILMerge?
Thank you in advance.
Error:
leave command
""C:\Users\User\Desktop\FastFoodDemo\ILMerge\merge_all.bat"
"C:\Users\User\Desktop\FastFoodDemo\"
"C:\Users\User\Desktop\FastFoodDemo\FastFoodDemo\bin\Debug\RCC.exe"
Debug" with code 1. FastFoodDemo
Event final assembly:
"$(SolutionDir)\ILMerge\merge_all.bat" "$(SolutionDir)" "$(TargetPath)" $(ConfigurationName)
merge_all.bat
#ECHO OFF
rem # set .NET version and output folder name
set net="v4, C:\Windows\Microsoft.NET\Framework\v4.0.30319"
set output=Output
rem # process arguments
set ILMergeSolution=%1ILMerge\ILMerge.exe
rem # determine programm files of x86 for 32 and 64 Platform
IF EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES(x86)%
IF NOT EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES%
rem # if ILMerge.exe not in the $(SolutionDir)ILMerge\
rem # then try to use installed in prorgammFiles
IF EXIST %ILMergeSolution% set ILMerge="%ILMergeSolution%"
IF NOT EXIST %ILMergeSolution% set ILMerge=%prorgammFiles%\Microsoft\ILMerge\ILMerge.exe
set target_path=%2
set target_file=%~nx2
set target_dir=%~dp2
set ConfigurationName=%3
rem # set output path and result file path
set outdir=%target_dir%%output%
set result=%outdir%\%target_file%
rem # print info
#echo Start %ConfigurationName% Merging %target_file%.
#echo Target: %target_path%
#echo target_dir: %target_dir%
#echo Config: %ConfigurationName%
rem # recreate outdir
IF EXIST "%outdir%" rmdir /S /Q "%outdir%"
md "%outdir%"
rem # run merge cmd
#echo Merging: '"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"'
"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"
rem # if succeded
IF %ErrorLevel% EQU 0 (
rem # clear real output folder and put there result assembly
IF %ConfigurationName%==Release (
del %target_dir%*.*
del %target_dir%*.dll
del %target_dir%*.pdb
del %target_dir%*.xml
del %target_dir%*.*
copy %result% %target_dir%
rmdir /S /Q %outdir%
set result=%target_path%
#echo Result: %target_file% "-> %target_path%"
) ELSE (
#echo Result: %target_file% "-> %result%" )
set status=succeded
set errlvl=0
) ELSE (
set status=failed
set errlvl=1
)
#echo Merge %status%
exit %errlvl%

c# and ffpmeg command line trouble

I have a php code that calls ffmpeg from the windows command line (because I run apache locally) with:
//ffmpeg -i input.mp4 -r 20 output.gif
echo exec('C:\ffmpeg\bin\ffmpeg.exe -i videos/file.avi -r 10 -s 450x230 videos/file.gif');
And it works fine! Now I wrote a C# program and I try to do the same thing with:
System.Diagnostics.Process.Start
but it fails.
Question: How do I execute the same command from the PHP code but from inside a C# code?
Process process = Process.Start(
new ProcessStartInfo
{
FileName = #"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = "-i videos/file.avi -r 10 -s 450x230 videos/file.gif",
WindowStyle = ProcessWindowStyle.Hidden
}
);
process.WaitForExit();
If it fails an exception will be raised, e.g.
System.ComponentModel.Win32Exception with HResult=-2147467259 if the executable wasn't found.

Enable Windows Firewall (with Advanced Security) logging

Which is the equivalent of:
netsh advfirewall set currentprofile logging filename %systemroot%\system32\LogFiles\Firewall\pfirewall.log
in C/C++ and VBScript ?
I found nothing in official: Windows Firewall with Advanced Security Interfaces
Note:
I am interested only in enabling/disabling logging, not in changing the log filename.
Don't know about C/C++, but in VBScript you'd simply shell out to run the netsh commandline:
logfile = "%systemroot%\system32\LogFiles\Firewall\pfirewall.log"
Function qq(str) : qq = Chr(34) & str & Chr(34) : End Function
Set sh = CreateObject("WScript.Shell")
sh.Run "netsh advfirewall set currentprofile logging filename " & qq(logfile)

Unable to run VB script on a remote server using c# Asp.Net

I am trying to Run DelTemp-Final.vbs on a remote server which is being imported from a web portal.
I tried to use native C# to get the task done, No luck. So I opted for PS tools to help me out. I am stuck with he below scenario. Please help
Psexec is working using command prompt, but not working with c#
Below is the command prompt output which is working fine:
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit1 -u XXXX\xxxxxxx -p xxxx
Xxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit1 with process ID 5452.
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit2 -u XXXX\xxxxxxx -p xxxxx
xxxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit2 with process ID 7416.
Whereas, the same command does not work when included with c# in asp.net
Process p=new Process();
p.StartInfo.UseShellExecute=false;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardError=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName=#"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments="\\\\"+li.Text+" -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.Start();
string output=p.StandardOutput.ReadToEnd();
string errormessage=p.StandardError.ReadToEnd();
p.WaitForExit();
txtValueA.Text +="PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text +="<br/> Output : " + output+"| error messsage: "+errormessage;
Output of the above code for two servers is as follows:
PSexec argument :\\testusit1 -u XXXX\xxxxxxxxxx -p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com The handle is invalid. Connecting to testusit1... Starting
PSEXESVC service on testusit1... Connecting with PsExec service on testusit1... Error deriving
session key:
PSexec argument :\\testusit2 -u XXXX\xxxxxxxxxxx-p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com Access is denied. Connecting to testusit2... Starting PSEXESVC
service on testusit2... Could not start PSEXESVC service on testusit2: Connecting to testusit2...
Starting PSEXESVC service on testusit2...
Please assist.. Is there any changes I need to do to the script? Any help is greatly appreciated.
Thanks a lot in advance :)
Try directing your output to an event handler instead. I just ran a variation of this on my server with psexec executing ipconfig on the remote machine and I was able to view all output.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments = "\\\\" + li.Text + " -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
txtValueA.Text += "PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text += "<br/> Output : " + output + "| error messsage: " + errormessage;
}
public static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
//Standard output event handler for PSEXEC
public static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

Categories