web-dev-qa-db-ja.com

Windowsでネットワーク共有を無効にするバッチファイルXP

この質問に大まかに関連しています ネットワーク共有によりCygwinが 'ls'の後に遅く実行されます 、ホストをネットワーク共有から切断し、その後別のバッチを切断するために実行できる小さなバッチファイルを作成したいと思います再接続するファイル。理想的には、これはcygwinを実行しているボックスにSSHで接続されたPuTTYターミナルから実行できるものです。

バッチファイルは簡単に書き込めると思いますが、PuTTY端末から実行する方法はわかりません。とにかく、とにかくバッチファイルが欲しいのですが。

簡単にするために、私のプロセスは次のようになります。

  1. PuTTY経由でサーバーにログインします
  2. バッチファイルを実行して共有を切断します
  3. 私がする必要があることをしなさい
  4. バッチファイルを実行して共有を再接続します
  5. セッションを終了し、PuTTYを閉じます
2
Robb

以下は、ローカルのAdministratorグループのアカウント、できればマップされている共有にアクセスできるアカウントで実行する必要があることに注意してください。


基本的なコマンド

ドライブを切断するには:

Net Use /d * /y

壊す:

  • Net Useは、コマンドラインからネットワークドライブマッピングを変更するためのユーティリティです。
  • /dは「削除」用で、コマンドパラメータで指定されているドライブマッピングをすべて切断します。
  • *はワイルドカードで、現在マップされているドライブでコマンドを実行しますall
  • /yは「はい」を表し、コマンドの対話型確認をバイパスします。

ドライブを再接続するには:

Net Use [driveletter]: "\\[servername]\[sharename]\[subfolder-path]" /p:y

(マッピングごとに繰り返します。)

壊す:

  • Net Use上記を参照)
  • [driveletter]:-これを使用したいドライブ文字に置き換えます。角かっこを削除し、コロンを保持します。共有をドライブ文字に関連付ける必要がない場合は、これを完全に削除できます。
  • "\\[servername]\-これを共有をホストしているマシンの名前またはIPに置き換えます。ブラケットを取り外します。バックスラッシュは保持します。パスにスペースが含まれていない場合、引用符はオプションです。
  • [sharename]-これをアクセスしている共有の名前に置き換えます。ブラケットを取り外します。
  • \[subfolder-path]-(オプション)これを、マッピングでアドレス指定するサブフォルダーへの残りのパスに置き換えます。マッピングが共有のルートを指すようにするだけの場合は、省略してください。ブラケットを取り外します。必要に応じてバックスラッシュを使用します。
  • "-開始引用符を保持した場合は、終了引用符を残します。それ以外の場合は削除します。
  • /p:y-これは「Persistent:YES」用です。つまり、ドライブマッピングは再起動しても保持されます。

コメント付きのサンプルバッチファイル

REM The first line below keeps the batch commands from "echoing" on the command line.  Only command output is displayed.  Delete or comment out that line for debugging.
@echo off
REM The next command deletes all drive mappings.
Net Use /d * /y
REM The next two lines print a message stating what the previous line should have done (check command output to verify) and what the user should do next.
echo Drive mappings DELETED!
echo Press any key to restore drive mappings.
REM The next line pauses the batch job, pending user input.  Leave the batch window open, and go on to do your work.  Return to the batch window and press any key to continue.
PAUSE
REM The next command is an example of connecting to a share path that does not include subfolders or spaces, and will not be mapped to a drive letter.
Net Use \\myserver\logs
REM This next command is an example of mapping a drive letter to a path that includes a subfolder, but no spaces.
Net Use R: \\myserver\myapp\reports /p:y
REM This next command is an example of mapping a drive letter to a path  that does include sub-folders and spaces.  Note the requisite quotation marks.
Net Use P: "\\myserver\c$\Program Files\My Application\"
REM The next two lines print messages similar to the previous two "echo" commands, this time informing the user that the drives should be re-mapped.
echo Drive mappings RESTORED!
echo Press any key to exit.
REM This last line inserts a final pause in the batch job.  Use this opportunity to check the command output and verify that the previous commands completed succesfully.
PAUSE
REM The batch window should automatically exit after this, or return to the command Prompt if the file was run from within an existing console.

これは、必要に応じて2つの別々のバッチファイルに簡単に分割でき、分割は最初のPAUSEの直後に配置されることに注意してください。バッチファイルが分割されている場合でも、バッチファイルの最後にPAUSEコマンドを保持して、バッチコマンドが終了する前に成功したことを確認できるようにすることを強くお勧めします。


別のユーザーとしてのマッピング

共有にアクセスできるユーザーとしてNet Useコマンドを実行できない場合は、マッピングごとに追加のパラメーターとユーザーとの対話が必要になります。

ドライブマッピングを削除するために追加のパラメータは必要ありません。

別のユーザーとしてドライブを再接続するには、各コマンドに次のパラメーターを追加します。

/user:[domain\username]

OR

/user:[username@domain]

例:

Net Use R: \\myserver\reports /user:mydomain\me /p:y

OR

Net Use P: "\\myserver\c$\Program Files\My Application" /user:mysubdomain.mydomain.tld\me /p:y

OR

Net Use \\myserver\logs /user:[email protected] /p:y

マッピングごとに、パスワードの入力を求められます。コマンドにパスワードを含めることができる追加のパラメーターがあると思います。ただし、バッチファイルはクリアテキストで保存されるため、私は---(強く使用しないことをお勧めします。

2
Iszi

これを試してください-Cygwinでどのように機能するかわかりません...しかし、これはBATCHであり、Windowsで通常実行された場合に機能するはずです。

REM Delete all existing connections
Net Use * /DELETE /Y
REM Map connections to the X, Y, Z letters for the shares
Net Use X: \\SERVER1\SHARE1
Net Use Y: \\SERVER1\SHARE2
Net Use Z: \\SERVER2\SHARE1
3
Multiverse IT