Is there a Powershell command to check if a file is in use by another user? If not, what would it take to write a script that can?
You can never tell if a file is currently being used only that it was recently being used. The reason why is that the moment the script returns the file could be closed by whatever program was previously using it. Writing scripts like this will only lead to flaky behavior.
A much better approach is to just do whatever the script was going to do if the file wasn't in use and catch the exceptions that result from a use conflict. The end result will be a much simpler and more reliable program.
There isn't a built-in command that I'm aware of however there are several tools you can use for this:
net file
From SysInternals on Technet (psfile and handle):
psfile.exe # lists and allows you to close remotely opened files
handle.exe | select-string ': File'
I find that using the SysInternals "ListDlls.exe" is pretty easy and convenient:
c:\SysInternals\Listdlls.exe -d AssemblyInUse.dll
ListDLLs v3.1 - List loaded DLLs
Copyright (C) 1997-2011 Mark Russinovich
Sysinternals - www.sysinternals.com
----------------------------------------------------------------------------
powershell.exe pid: 7296
Command line: "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"
I hope that helps!
Related
Does anybody know how can I create a text file in OpenVMS without the version extension? I need to take some logs from this server and edit them in a Linux server. Until know the downloaded file from OpenVMS server cannot be opened.
Regards,
Theodore
Did you google your problem? You would find answers to help you along.
As indicated you cannot get rid of the file version number on the OpenVMS side.
You can edit those file on the Linux side easily - folks do it all the time.
You indicate the file cannot be opened. What is the error message?
Best is to copy&paste the exact command used and the returned message.
On Linux You probably just need to escape the ";" with a backslash to avoid it from terminating the command fragment. Or you can put the whole name in quotes.
If you don't like that, then you may need to look at the tool used to access (Samba?), package (zip?) or transfer the file (ftp/sftp). They are likely to have a setting to honor or discard the file version.
Since you fail to indicated exactly what you are doign, we cannot help until we know.
The ZIP on my OpenVMS server has options -
zip -h
Info-ZIP - Zip 3.0 (July 5th 2008).
-w store file version numbers -ww store file version numbers as ".nnn"
Good luck!
Hein
I want to kill RunDll32 process which is started from my install directory.
So if I use
${nsProcess::KillProcess} "rundll32.exe" $R0
It kills all the rundll32 processes on the system which I don't want to happen.
IMO, I have two options to fix this,
1. Identify interested process from commandline parameters
2. Identify from process startup directory (current directory).
I see there are few plugins to find the process but what they do is they just return found or not found. Instead I want IDs of the processes or list of these processes and then I'll check each process for command line or startup directory information and will act on the the required process.
BTW, I checked following plugins
http://nsis.sourceforge.net/FindProcDLL_plug-in
http://nsis.sourceforge.net/Processes_plug-in
http://forums.winamp.com/showthread.php?t=230998
Thanks
I fixed by using wmic query as follows:
StrCpy $1 "wmic Path win32_process where $\"name like 'Rundll32.exe' and CommandLine like '%$0%'$\" Call Terminate"
nsExec::Exec $1
If you control the .dll then the best option is to provide some sort of way to shutdown the app in a clean way. Perhaps you could find a window based on its class name and send it a WM_CLOSE message.
If you just need to shutdown the application during upgrade/uninstall then the LockedList plug-in is much better than just killing processes...
I am new to WinSCP and am attempting to create a script file that will eventually be used with SSIS to download files from an SFTP site. A lot of the literature WinSCP includes explains the file downloading or uploading portions. For the time being, I just want to create a script to test the connection first and will build from there.
So far I saved the connection in WinSCP and have the following. The below code does not seem to function at all and I am not sure where else to go as I am still reading about the scripting for WinSCP. Is there a way or can someone point me in a direction to see if I am in fact connecting via through the script?
option batch on
option confirm off
open username#address
exit
Not sure what SSIS is (sorry) but I can tell you how I'd set it up from a windows batch file if that helps:
If you are open to using a different software, consider using cygwin. It mimics a linux shell so linux users on windows have a lot of linux utilities handy. That being said, there are some commands which can run on windows straight from command prompt (and thus batchable). What you'd need to do:
1) install cygwin
2) Create a "passwordless" login (using ssh-rsa authentication). To do this start your cygwin terminal and use the commands "ssh-keygen" and "ssh-copy-id" (more on that later)
3) Now you can run "sftp" from the DOS command prompt (does not require cygwin terminal) and sftp to your account. No password required because of step 2).
A few follow up info:
What can run from dos command prompt and what must be run from cygwin terminal?
If you go to the "bin" directory of cygwin (for me it's in c:\cygwin\bin) you can see all the cygwin utilities. Anything with "exe" extension can be run from dos command prompt. If no "exe" extension, must start cygwin terminal first
How to set up ssh-rsa authentication?
You can pretty much google "ssh login without password" and pull up a lot of results. This is common for setting up login from one linux system to another. You would be using the same steps using cygwin on windows. My instructions are here:
http://geekswing.com/geek/unix/how-to-ssh-login-without-a-password-using-ssh-keygen-quick-tutorial/
Storing session settings in WinSCP GUI and trying to access them from WinSCP script running in SSIS is generally a bad idea. I believe there's no example or guide on WinSCP site that would suggest doing that.
WinSCP stores its configuration in registry in HKEY_CURRENT_USER hive. The SSIS typically runs under a dedicated system account, that have its own HKEY_CURRENT_USER hive, and won't see the GUI configuration.
For details see WinSCP FAQ about your problem:
https://winscp.net/eng/docs/faq_scheduler
The best you can do is isolate your your script from configuration by using the session URL with the open command, instead of the stored site name.
See also https://winscp.net/eng/docs/scripting#configuration
Your actual problem can be completely different though. But that's hard to guess as you have not shared any details, such as error message, log file, etc.
On windows and such I used to use a trick to find out of a file is currently in use (written specifically).
I use to open the file for writing and if it failed most likey another process is busy accessing it.
Sadly these trick (using C OPEN with exclusive lock for writing) doesn't work on my Mac. While my curl in a terminal is still writing my -fileBusy() check fails.
fcnt call on the fd with F_GETLK doesn't reveal any locking as well.
Is there any chance for me to detect if a file is in use by another process?
Ps> listening for fsevents can't be done because my app launches after the is created by the other app / process.
Apple confirmed via email that the solution described in the link below is a valid one and not considered a private API.
More information:
http://lists.apple.com/archives/cocoa-dev/2010/May/msg01455.html
You could try running the following shell command using NSTask:
lsof -Fc path/to/thefile
That will give you the process ID and name of any process(es) that have thefile open.
I'm working on an app that needs to use dd (I do this with a shell script in the app bundle, that collects parameters from the app itself, makes some checks and then launches dd).
To make this operation I need to call dd with root, and I already looked at several solutions on StackOverflow. The simplest to implements seemed to me this one http://www.sveinbjorn.org/STPrivilegedTask
Problem is that my NSTask makes some complex read/write operations (not present in STPrivilegedTask) and does not need to be all privileged.
So I wrote a small helper tool in c that calls my script with correct parameters from my app. The solution I thought is to use the STPrivilegedTask to SUID once the fly my small helper tool, so I can launch it (and so my script and dd) with root, and soon after successful launch I set back the helper tool to non SUID (and I do the same if any error, on app exit, app start etc.. to be safer).
I implemented it and works quite well, maybe it's not perfect but I think that being all inside the bundle, and working with the helper tool in SUID just for the launch sounds safe enough.
Any thoughts?
Thanks!
You can use a sandbox for running the new Process in your NSTask
sandbox-exec -f <profile> <command>
sandbox-exec -f my_profile.sb "/bin/dd -if=/dev/disks01 of=/dev/target"
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/sandbox-exec.1.html
You have some profile examples in here
/usr/share/sandbox/
You have to give enough access for dd to work, I haven't tried or checked what dd requires, I would start with something like this:
(version 1)
(deny default)
(debug deny)
(import "system.sb")
(allow file-read-data file-write-data file-ioctl (regex #"^/dev/.*$"))
(allow process-exec (literal "/usr/sbin/helper"))
Update:
Worth mention, you can use
sandbox-exec -p command