2013-04-26 139 views
0

製造這個腳本:將數據導出到文本文件

#!/usr/local/bin/perl 

#simple export from win-services with current state v. 0.1 

use Win32::Service; 

#hash-code for service reasons 
my %statcodeHash = (
    '1' => 'STOPPED', 
    '4' => 'RUNNING', 
    '7' => 'PAUSED' 
); 

my %serviceHash; 


#Going to get the data from services and export 

Win32::Service::GetServices("", \%serviceHash); 

foreach $key(keys %serviceHash){ 
    my %statusHash; 
    Win32::Service::GetStatus("", $serviceHash{$key}, \%statusHash); 
    if ($statusHash{"CurrentState"} =~ /[1-7]/){ 
      print $serviceHash{"$key"} . "THE SERVISE IS " . $statcodeHash{$statusHash{"CurrentState"}} . "\n"; 



    } 
} 

我怎麼能忍受那些scrips給我,並把它放在txt文件我的硬盤驅動器上的數據?

這些腳本顯示了我的系統中有哪些服務以及哪些服務正在運行,哪些服務未運行。 我要導出這.txt文件

感謝

回答

0

,而不是印刷STDOUT你應該把它打印到寫入/追加的文件。在參閱perldoc

perldoc -f open 
perldoc -f print 
+0

其印刷只有第一個字符串

# initialize outside foreach loop, open file for appending open MYFILE, '>>', '/path/to/my/txtfile' or die("error writing to file $!"); # print to file print MYFILE $serviceHash{"$key"} . "THE SERVISE IS " . $statcodeHash{$statusHash{"CurrentState"}} . "\n"; 

更多的文檔,e.c只有一個服務。爲什麼不alL ??? – 2013-04-26 09:12:34

+0

@Denis你可以轉儲幾行%serviceHash嗎? – erickb 2013-04-26 09:18:41

+0

erickb,我代替我符合你的,它是建立txt文件,但我看到的只有這個 WMPNetworkSvcTHE者地位IS RUNNING 但是,如果你用我的命令,它showes你約45服務,在CMD行,但在.txt中只有一個... – 2013-04-26 09:25:29